1. Create a Spring Initializer project Idea
Add Web and create a project in Lombok dialog box, or after the establishment of the project pom.xml
to add dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
2. Use annotations to build RESTful API
Here there is no database with ArrayList as the operation target.
Create an entity class, as the object API operations
package top.cloudli.demo.model;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class Character {
private int id;
private String name;
private String racial;
private String[] ability;
}
Lombok used herein, eliminating the need to write Getter, Setter and constructors, add the appropriate annotation will be automatically generated after lombok.
Creating Controller, implement RESTful API
@RestController
Equivalent @Controller
and @ResponseBody
a combination of the use of the annotation, MIME default application/json
.
@GetMapping
Equivalent @RequestMapping(method = {RequestMethod.GET})
, similar to other comments.
package top.cloudli.demo.controller;
import org.springframework.web.bind.annotation.*;
import top.cloudli.demo.model.Character;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* CRUD for Character
*/
@RestController
@RequestMapping(path = "/character")
public class CharacterController {
private ArrayList<Character> characters = (ArrayList<Character>) Stream.of(
new Character(1, "罗小黑", "妖精", new String[]{"空间系-传送", "空间系-领域"}),
new Character(2, "罗小白", "人类", null)
).collect(Collectors.toList());
/**
* 获取所有 Character
* @return All Characters
*/
@GetMapping()
public List<Character> getCharacters() {
return characters;
}
/**
* 根据 id 获取 Character
* @param id Id of Character
* @return Character
*/
@GetMapping(path = "{id}")
public Character getCharacter(@PathVariable int id) {
return characters.stream()
.filter(character -> character.getId() == id)
.findAny()
.orElse(null);
}
/**
* 更新 Character
* @param character Changed Character
* @return Updated Character
*/
@PutMapping()
public Character alterCharacter(@RequestBody Character character) {
AtomicBoolean found = new AtomicBoolean(false);
characters.replaceAll(c -> {
if (c.getId() == character.getId()) {
found.set(true);
return character;
} else return c;
});
return found.get() ? character : null;
}
/**
* 添加 Character
* @param character New Character
* @return Inserted Character
*/
@PostMapping()
public Character addCharacter(@RequestBody Character character) {
return characters.add(character) ? character : null;
}
/**
* 删除 Character
* @param id Id of Character
* @return Id of Character deleted
*/
@DeleteMapping(path = "{id}")
public int deleteCharacter(@PathVariable int id) {
return characters.removeIf(character -> character.getId() == id) ? id : -1;
}
}
3. Run the project, access to the API
URL | method | operating |
---|---|---|
/character | GET | Get all of Character |
/character/id | GET | Gets Character specified id |
/character | PUT | Complete modified object Character, modify transfer |
/character/id | DELETE | Delete the specified id Character |
If ?id=xxx
manner, the annotation may be path
parameters removed.
Sending GET
a request to http://localhost:8080/character
:
[
{
"id": 1,
"name": "罗小黑",
"racial": "妖精",
"ability": [
"空间系-传送",
"空间系-领域"
]
},
{
"id": 2,
"name": "罗小白",
"racial": "人类",
"ability": null
}
]
Sending GET
a request to http://localhost:8080/character/1
:
{
"id": 1,
"name": "罗小黑",
"racial": "妖精",
"ability": [
"空间系-传送",
"空间系-领域"
]
}
DELETE
With the above request GET
, as specified id after the success of Character will be deleted, delete the return of id.
Sending PUT
a request to ` http://localhost:8080/character
added to the Header Content-Type:application/json
, Body portion of the modified data is complete:
{
"id": 1,
"name": "小黑",
"racial": "妖精",
"ability": [
"空间系-传送"
]
}
After a successful return as above JSON data.
POST
Previous request PUT
as submitted after a successful return data.