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";
}
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";
}
You should consider to use HTTP status codes to indicate whether or not the delete operation succeeds rather than redirects. 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, etc. Based on the response status, the client can decide whether or not to keep the referenced resource (in your case the object that is referenced by item/{id}
).
Since you are using Spring, you can annotate your controller methods with an appropriate @ResponseStatus, e.g.
@ResponseStatus(value = HttpStatus.NO_CONTENT)
Additionally, Spring HandlerExceptionResolver
already provides some status codes by default.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With