In the previous post, Build a Next Level RESTful Web Service in Spring Boot 2, we took the RESTful web service that you create with the Spring Guide and added the ability to change the greeting portion with a PUT request.
PUT is just one of many HTTP methods that can be used for HTTP requests. There are also POST, DELETE, OPTION, and HEAD methods. You could check out the HTTP specifications to learn the technical meanings for each of these. But perhaps one of the best resources for learning the semantics of these methods is Learn REST: A RESTful tutorial. Here’s a quick summary from there:
Generally, the four primary HTTP verbs are used as follows:
GET
Read a specific resource (by an identifier) or a collection of resources.PUT
Update a specific resource (by an identifier) or a collection of resources. Can also be used to create a specific resource if the resource identifier is known before-hand.DELETE
Remove/delete a specific resource by an identifier.POST
Create a new resource. Also a catch-all verb for operations that don’t fit into the other categories.
Mappings in Spring Boot 2
So how do you apply that in Spring Boot 2? It’s probably best to show an example.
@RestController
@RequestMapping("/smartphones")
public class SmartphoneController {
List<Smartphone> phones;
public SmartphoneController() {
this.phones = new ArrayList<>();
}
@PostMapping
public String addNewPhone(@RequestBody Smartphone smartphone) {
phones.add(smartphone);
return "OK";
}
@DeleteMapping
public String deletePhone(@RequestBody Smartphone smartphone) {
phones.remove(smartphone);
return "OK";
}
@GetMapping
public List<Smartphone> getPhones() {
return phones;
}
}
I walk through creating this example in the following video.
Let's walk through this example. First, you can see that this controller class has a RequestMapping annotation at the class level (line 2). This is a common recommended practice which assigns responsibility for the endpoint ("/smartphones" in this case) to the class:
@RestController
@RequestMapping("/smartphones")
public class SmartphoneController {
Next, Spring Web MVC (it's actually part of Spring framework generally rather than specifically being part of Spring Boot) actually provides a mapping for each of the verbs as an annotation you can put on different methods. They all start with the HTTP verb (like Delete) and then the word mapping: PostMapping, PutMapping, GetMapping, DeleteMapping, etc. You can see the use of three of these on lines 10, 16, and 22 in this example.
When you annotate a method with one of these mappings, Spring will match up incoming requests to that method. Let’s take the "addNewPhone" method here on line 11. When Spring sees an HTTP request come in with the POST verb and it was sent to the "/smartphones" endpoint, Spring resolves this method as the method responsible for it.
@PostMapping
public String addNewPhone(@RequestBody Smartphone smartphone) {
phones.add(smartphone);
return "OK";
}
In the "addNewPhone" method, we take an HTTP request that is a POST request sent to the "/smartphones" endpoint, and look in the request body for JSON that matches the Smartphone class. The JSON looks like this:
{
"brand": "Apple",
"product": "iPhone"
}
In this example, we simply add it to a list of phones and then return the String value "OK."
The other methods similarly manage the list of phones. You can get the full list or delete a phone from the list. The video shows all of this in action and talks through it in more detail.
And that's really about it! You can read a lot more information about this in the Spring reference documentation, but in practice Spring makes it very easy to add new endpoints with these annotations.
Top comments (1)
Kudos to you for this article and the ones before it. Well explained and to the point.