RESTful Mappings in Spring Boot 2
Tags: java, springboot, webdev
Published: 2020-05-14 15:50:51

---
title: RESTful Mappings in Spring Boot 2
published: true
description: Learn how to put RESTful mappings on the controller methods of RESTful web services in Spring Boot 2
tags: java,springboot,webdev
---

In the previous post, [Build a Next Level RESTful Web Service in Spring Boot 2](https://dev.to/scottshipp/build-a-next-level-restful-web-service-with-spring-boot-20d4), we took the RESTful web service that you create with the [Spring Guide](https://spring.io/guides/gs/rest-service/) 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](https://developer.mozilla.org/en-US/docs/Web/HTTP/Resources_and_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](https://www.restapitutorial.com/). 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.

```java
@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.

{% youtube D1FB7HpPFDE %}

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:

```java
@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.

```java
    @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:

```javascript
{
   "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](https://docs.spring.io/spring/docs/5.3.0-SNAPSHOT/spring-framework-reference/web.html#mvc-controller), but in practice Spring makes it very easy to add new endpoints with these annotations.