DEV Community


Posted on • Originally published at

RESTful Mappings in Spring Boot 2

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:

Read a specific resource (by an identifier) or a collection of resources.

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.

Remove/delete a specific resource by an identifier.

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.

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:

public class SmartphoneController {
Enter fullscreen mode Exit fullscreen mode

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.

    public String addNewPhone(@RequestBody Smartphone smartphone) {
        return "OK";
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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.

Discussion (1)

bertilmuth profile image
Bertil Muth

Kudos to you for this article and the ones before it. Well explained and to the point.