REST and the MVC pattern

The Spring Web MVC module provides an implementation of the traditional Model View Controller pattern. While REST does not mandate the use of any specific pattern, using the MVC pattern is quite a natural fit whereby the RESTful resource or model is exposed through a controller. The view in our case will be a JSON representation of the model.

Without further ado, let's take a look at our first endpoint:

@RestController
@RequestMapping("/rooms")
public class RoomsResource {

  private final InventoryService inventoryService;

  public RoomsResource(InventoryService inventoryService) {
    this.inventoryService = inventoryService;
  }

  @RequestMapping(value = "/{roomId}", method = RequestMethod.GET) public RoomDTO getRoom(@PathVariable("roomId") ...

Get Building a RESTful Web Service with Spring now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.