如何在Spring MVC中处理RESTful删除 [英] How to handle RESTful delete in Spring MVC

查看:124
本文介绍了如何在Spring MVC中处理RESTful删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Spring MVC控制器中正确使用RESTful删除?我有DAO在尝试删除某个项目时返回布尔值。

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.

我是否需要一些特殊的响应状态或类似的内容?我的方法是RESTful吗?

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


推荐答案

如果您遇到问题删除可以由用户修复,然后这似乎没问题。如果用户无法做任何事情,那么错误代码状态可能更正确。我可以想象删除的唯一失败是授权失败,这将是一个401.这可以通过在方法'HttpServletResponse response'中添加一个参数来设置。您的代码将类似于:

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

这篇关于如何在Spring MVC中处理RESTful删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆