How to handle RESTful delete in Spring MVC
java, rest, spring, spring-mvc
Solution
If you the issues with a delete can be fixed by the user, then this seems ok. If there is nothing the user can do then perhaps an error code status would be more correct. The only failure I can imagine for a deletion would be an authorization failure, which would be a 401. This could be set by adding a parameter to your method 'HttpServletResponse response'. Your code would 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, f.e. 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 substitue other status codes as appropriate, but that is the General idea.
You could also do something like:
if (!wasOk) {
throw new DataAccessException("Unable to delete item: " + item);
}
And then have an annotated error handler in the same class
@ExceptionHandler(DataAccessException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public String handleDataAccessException(DataAccessException ex) {
// Do some stuff
return "errorView";
}
Problem
How do I correctly use RESTful delete in Spring MVC controller? I have DAO that returns boolean when trying to delete some item. I am trying to delete item. If everything was OK, just show list of items (deleted item won't be there anymore). If item cannot be removed, redirect to details page and say why it couldn't 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, f.e. 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"; } ```