Creating resources

The inventory component of our sample property management system deals with rooms. In Chapter 3, The First Endpoint, we built an endpoint to access rooms. Let's take a look at how to define an endpoint for creating new resources:

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

  @RequestMapping(method = RequestMethod.POST)
  public ApiResponse addRoom(@RequestBody RoomDTO room) {
    Room newRoom = createRoom(room);
    return new ApiResponse(Status.OK, new RoomDTO(newRoom));
  }
}

We've added a new method to our RoomsResource class to handle the creation of new rooms. As described in Chapter 3, The First Endpoint, @RequestMapping is used to map requests to the Java method. Here, we map POST requests to addRoom().

Tip ...

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.