Skip to content

7.1 Writing RESTful controllers

In a nutshell, REST APIs aren’t much different from websites. Both involve responding to HTTP requests. But the key difference is that instead of responding to those requests with HTML, as websites do, REST APIs typically respond with a data-oriented format such as JSON or XML.

In chapter 2 you used @GetMapping and @PostMapping annotations to fetch and post data to the server. Those same annotations will still come in handy as you define your REST API. In addition, Spring MVC supports a handful of other annotations for various types of HTTP requests, as listed in table 7.1.

Table 7.1 Spring MVC’s HTTP request-handling annotations

AnnotationHTTP methodTypical use
@GetMappingHTTP GET requestsReading resource data
@PostMappingHTTP POST requestsCreating a resource
@PutMappingHTTP PUT requestsUpdating a resource
@PatchMappingHTTP PATCH requestsUpdating a resource
@DeleteMappingHTTP DELETE requestsDeleting a resource
@RequestMappingGeneral-purpose request handling; HTTP method specified in the method attribute

To see these annotations in action, you’ll start by creating a simple REST endpoint that fetches a few of the most recently created tacos.

Released under the MIT License.