Book Image

Building a RESTful Web Service with Spring

By : Ludovic Dewailly
Book Image

Building a RESTful Web Service with Spring

By: Ludovic Dewailly

Overview of this book

Table of Contents (17 chapters)
Building a RESTful Web Service with Spring
About the Author
About the Reviewers

Deleting resources

It will come as no surprise that we use the DELETE verb to delete REST resources. Also, you will have already figured out that the path to delete requests is /rooms/{roomId}.

The Java method that deals with room deletion is shown as follows:

@RequestMapping(value = "/{roomId}", method = RequestMethod.DELETE)
public ApiResponse deleteRoom(@PathVariable long roomId) {
  try {
    Room room = inventoryService.getRoom(roomId);
    return new ApiResponse(Status.OK, null);
  } catch (RecordNotFoundException e) {
    return new ApiResponse(Status.ERROR, null, new ApiError(999, "No room with ID " + roomId));

By declaring the request mapping method to be RequestMethod.DELETE, Spring will make this method handle DELETE requests.

Since the resource is deleted, returning it in the response would not make a lot of sense. Service designers may choose to return a Boolean flag to indicate that the resource was successfully deleted. In our...