1. Use Idea to create Spring Initializer project
Add Web and Lombok in the create Project dialog box, or add dependency in pom.xml after the project is created:
<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. Build RESTful API with annotations
The database is not used here, and ArrayList is used as the operation object.
Create an entity class as an object for 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 is used here, instead of writing Getter, Setter and constructor. lombok will be generated automatically after corresponding annotation is added.
Create Controller and implement RESTful API
@RestController is equivalent to the combination of @ Controller and @ ResponseBody. After using this annotation, MIME defaults to application/json.
@GetMapping is equivalent to @ RequestMapping(method = {RequestMethod.GET}), and other annotations are similar.
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, "Luo Xiao He", "Fairy", new String[]{"Space system-Delivery", "Space system-field"}), new Character(2, "Luo Xiao Bai", "Human beings", null) ).collect(Collectors.toList()); /** * Get all characters * @return All Characters */ @GetMapping() public List<Character> getCharacters() { return characters; } /** * Get Character according to id * @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); } /** * Update 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; } /** * Add Character * @param character New Character * @return Inserted Character */ @PostMapping() public Character addCharacter(@RequestBody Character character) { return characters.add(character) ? character : null; } /** * Delete 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 and access the API
URL | Method | operation |
---|---|---|
/character | GET | Get all characters |
/character/id | GET | Get the Character of the specified id |
/character | PUT | Modify Character to transfer the modified full object |
/character/id | DELETE | Delete Character with specified id |
If you use the method of? id=xxx, you can remove the path parameter from the annotation.
Send GET request to http://localhost:8080/character:
[ { "id": 1, "name": "Luo Xiao He", "racial": "Fairy", "ability": [ "Space system-Delivery", "Space system-field" ] }, { "id": 2, "name": "Luo Xiao Bai", "racial": "Human beings", "ability": null } ]
Send GET request to http://localhost:8080/character/1:
{ "id": 1, "name": "Luo Xiao He", "racial": "Fairy", "ability": [ "Space system-Delivery", "Space system-field" ] }
The DELETE request is the same as the GET above. After success, the Character with the specified id will be deleted and the deleted id will be returned.
Send a PUT request to 'http://localhost:8080/character, add content type: application / JSON to the Header, and the Body part is the modified complete data:
{ "id": 1, "name": "Small black", "racial": "Fairy", "ability": [ "Space system-Delivery" ] }
After success, the same JSON data as above will be returned.
The POST request is the same as the previous PUT, and the submitted data will be returned after success.