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") String roomId) { RoomDTO room = ... // omitted for sake of clarity return room; } }
With the use of @org.springframework.web.bind.annotation.RestController
, we instruct Spring...