Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle RESTful delete in Spring MVC

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";
}
like image 486
Xorty Avatar asked Apr 26 '12 20:04

Xorty


2 Answers

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";
}
like image 181
Kodi Avatar answered Oct 29 '22 14:10

Kodi


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 HandlerExceptionResolveralready provides some status codes by default.

like image 19
matsev Avatar answered Oct 29 '22 15:10

matsev