java HTTP 状态 405 - 在 spring mvc 中返回“redirect:*.*”时不支持请求方法“GET”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32986640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:02:13  来源:igfitidea点击:

HTTP Status 405 - Request method 'GET' not supported when doing return "redirect:*.*" in spring mvc

javaspringspring-mvc

提问by romil gaurav

In spring MVC... I have to reload a page after the record is updated. So in the action method I am returning String and in return I am returning

在 spring MVC 中...我必须在记录更新后重新加载页面。所以在 action 方法中我返回 String 而作为回报我返回

 return "redirect:/recordList";

When but it is giving the exception on the page

当但它在页面上给出异常

 HTTP Status 405 - Request method 'GET' not supported

There is no exception coming on the logs.

日志上也不例外。

My controller method looks like

我的控制器方法看起来像

    @RequestMapping(value= "/recordList", method = RequestMethod.POST)
    public ModelAndView getAssetListForUser(@RequestParam("ldapId") String ldapId,
        final HttpServletRequest request){

Also, because I need the value if ldapId in the controller method, I am binding it in the calling method like this

另外,因为我需要控制器方法中的 ldapId 值,所以我将它绑定到调用方法中

     request.setAttribute("ldapId", assetAssetEmp.getAssetEmpId());

Please help.

请帮忙。

回答by Raja Nadar

It looks like your /recordListcontroller method only supports RequestMethod.POST. hence you're getting the 405 GET Method not allowed error, since the redirect will issue a GET request.

看起来您的/recordList控制器方法仅支持RequestMethod.POST. 因此您会收到 405 GET Method not allowed 错误,因为重定向将发出 GET 请求。

to solve for it, try to give a GET version of your controller action. (the assetlist method) so that once the update happens, the asset list of the user can be returned via the GET method.

要解决它,请尝试提供控制器操作的 GET 版本。(资产列表方法),以便一旦发生更新,可以通过 GET 方法返回用户的资产列表。

回答by Ankur Singhal

The @RequestMappingannotation is your way to specify when your method is going to be called in an annotated controller. The RequestMethod.GETand RequestMethod.POSTarguments allow you to respond to the specific HTTPrequest type.

@RequestMapping注释是你当你的方法是要在注释控制器被称为指定的方式。在RequestMethod.GETRequestMethod.POST参数允许您具体回应HTTP请求类型。

providing method = RequestMethod.POSTto controller method actually making method to accept only HTTP POSTrequests.

提供method = RequestMethod.POST给控制器方法实际上使方法只接受HTTP POST请求。

RequestMethod

请求方法