How to handle RESTful delete in Spring MVC

How to use RESTful delete correctly in Spring MVC controller? I have a DAO that returns boolean when I try to delete an item.

I am trying to delete an item. If everything is ok, just show the list of items (the deleted item will no longer be there). If the item cannot be deleted, go to the details page and say why it could not be deleted.

Do I need some special response status or something like this? Is my approach RESTful?

@RequestMapping(value = "items/{id}", method = RequestMethod.DELETE) public String delete(@PathVariable("id") int itemId, Model model) { Item item = itemDao.get(id); // true -> can delete // false -> cannot delete, fe is FK reference somewhere boolean wasOk = itemDao.delete(item); if (wasOk) { return "redirect:/items"; } // will write to user which item couldn't be deleted model.addAttribute("item", item); return "items/error"; } 
+7
source share
2 answers

If the problems associated with the removal can be fixed by the user, then this looks normal. If the user cannot do anything, the error code may be more correct. The only failure that I can imagine for deletion is an authorization failure, which would be 401. This can be set by adding a parameter to your "HttpServletResponse response" method. Your code will become something like:

 @RequestMapping(value = "items/{id}", method = RequestMethod.DELETE) public String delete(@PathVariable("id") int itemId, Model model, HttpServletReponse response) { Item item = itemDao.get(id); // true -> can delete // false -> cannot delete, fe is FK reference somewhere boolean wasOk = itemDao.delete(item); if (!wasOk) { // will write to user which item couldn't be deleted response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); model.addAttribute("item", item); return "items/error"; } return "redirect:/items"; } 

You can replace other status codes if necessary, but this is a general idea.

You can also do something like:

  if (!wasOk) { throw new DataAccessException("Unable to delete item: " + item); } 

And then the annotated error handler in the same class

 @ExceptionHandler(DataAccessException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) public String handleDataAccessException(DataAccessException ex) { // Do some stuff return "errorView"; } 
+10
source

You should consider using HTTP status codes to indicate whether the delete operation succeeds, rather than being redirected. For example, HTTP 200 OK (or HTTP 204 No Content ) to indicate that the operation was successful, and HTTP 404 Not Found if the resource you are trying to access does not exist, HTTP 405 Method Not Allowed if the delete operation is not allowed and etc. Based on the response status, the client can decide whether to save the reference resource (in your case, the object referenced by item/{id} ).

Since you are using Spring, you can annotate controller methods with @ResponseStatus , for example. @ResponseStatus(value = HttpStatus.NO_CONTENT)

In addition, Spring HandlerExceptionResolver already provides some default status codes.

+16
source

All Articles