How to handle RESTful delete in Spring MVC

拟墨画扇 提交于 2020-01-21 01:42:46

问题


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";
}

回答1:


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";
}



回答2:


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.



来源:https://stackoverflow.com/questions/10340599/how-to-handle-restful-delete-in-spring-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!