java 获取“405 - 调用时不支持请求方法‘GET’”method=DELETE

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11471604/
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-10-31 05:15:19  来源:igfitidea点击:

Getting "405 - Request method 'GET' not supported when calling" method=DELETE

javahttpservletsspring-mvc

提问by Pete

I have a Spring MVC web app. In it a form with a button that's supposed to delete a resource from another resource:

我有一个 Spring MVC Web 应用程序。其中有一个带有按钮的表单,该按钮应该从另一个资源中删除一个资源:

<td>
    <form action="/product-bases/${productBase.id}/positions/${position.id}" method="DELETE">
        <input type="submit" value="delete" />
    </form>
</td>

My controller:

我的控制器:

@Controller
@RequestMapping(value = "/product-bases/{id}/positions")
public class ProductBasePositionController {

    @RequestMapping(value = "/{positionId}", method = RequestMethod.DELETE)
    public ModelAndView delete(@PathVariable Integer productBaseId, @PathVariable Integer positionId) {

So in theory the server should route to the controller. But alas it does not, hence the post ;)

所以理论上服务器应该路由到控制器。但可惜它没有,因此帖子;)

I'm getting

我越来越

HTTP Status 405 - Request method 'GET' not supported

type Status report

message Request method 'GET' not supported

description The specified HTTP method is not allowed for the requested resource (Request method 'GET' not supported).
Apache Tomcat/7.0.19

Obviously I don't have a get for /positions/id defined yet, but why should I, I want to do a delete for now..

显然我还没有定义 /positions/id 的 get,但是我为什么要这样做,我现在想删除..

(I'm also trying to run this from my spring-test-mvc framework on a mock servlet without any tomcat implementation in between and it gives me a 400 - bad request.. )

(我还试图在模拟 servlet 上从我的 spring-test-mvc 框架运行它,中间没有任何 tomcat 实现,它给了我一个 400 - 错误的请求..)

So what am I missing here?

那么我在这里错过了什么?

Oh, just to cut some corners: post and get will work for other resources, so the rest of my setup is fine.

哦,只是为了偷工减料:post 和 get 将适用于其他资源,所以我的其余设置很好。

The booting server even tells me:

启动服务器甚至告诉我:

RequestMappingHandlerMapping [INFO] Mapped "{[/product-bases/{id}/positions/{positionId}],methods=[DELETE],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.web.servlet.ModelAndView our.view.controller.ProductBasePositionController.delete(java.lang.Integer,java.lang.Integer)

Anyone as confused as I am? If less so, please enlighten me!

有和我一样困惑的吗?如果不是这样,请赐教!

回答by Simon Richter

Forms can be submitted via GETor POSTonly (maybe also PUT, but I doubt that is widely implemented), as form submission requires a method where data is transmitted to the server.

表单可以通过GETPOST仅提交(也许也可以PUT,但我怀疑它是否被广泛实施),因为表单提交需要一种将数据传输到服务器的方法。

The DELETEmethod does not have a request body, so specifying it in a form action is unsupported.

DELETE方法没有请求正文,因此不支持在表单操作中指定它。

回答by electrotype

Do you have the HiddenHttpMethodFilter filter in your web.xml?

你的 web.xml 中有 HiddenHttpMethodFilter 过滤器吗?

<filter>
    <filter-name>hiddenHttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>hiddenHttpMethodFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

http://static.springsource.org/spring/docs/current/spring-framework-reference/html/view.html#rest-method-conversion

http://static.springsource.org/spring/docs/current/spring-framework-reference/html/view.html#rest-method-conversion

回答by Stephen C

The error messages indicate that the browser is actuallysending a GET request rather than a DELETE request.

错误消息表明浏览器实际上发送的是 GET 请求而不是 DELETE 请求。

What you need to do is:

你需要做的是:

  • examine the source of the web page that the browser is on at the time, and

  • using the browser's web debugger, see what the request URL and method actually are.

  • 检查浏览器当时所在网页的来源,以及

  • 使用浏览器的 Web 调试器,查看请求 URL 和方法实际上是什么。

回答by Shubham Das

In Microsoft Azure portal i got this error for a Java App when i turned on Authentication/Authorization. If it's the same problem in your case, revert your action.

在 Microsoft Azure 门户中,当我打开身份验证/授权时,我收到了 Java 应用程序的此错误。如果在您的情况下存在相同的问题,请恢复您的操作。