java - 调用时不支持获取 "405 - Request method ' GET'method=DELETE

标签 java http servlets spring-mvc

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

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

我的 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) {

所以理论上服务器应该路由到 Controller 。但遗憾的是它没有,因此发布了这篇文章 ;)

我得到了

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

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

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

那么我在这里缺少什么?

哦,只是为了走捷径:post 和 get 将适用于其他资源,所以我的其余设置没问题。

启动服务器甚至告诉我:

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)

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

最佳答案

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

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

关于java - 调用时不支持获取 "405 - Request method ' GET'method=DELETE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11471604/

相关文章:

java - 为什么我需要结合 BeanListProcessor 调用 parseLine 两次

c# - 从静态方法中使用 IoC 容器解析类型

http - 如何在发送请求体之前检查是否可以执行 PUT?

java - 如何强制 sevlet 将字符串作为附件发送到浏览器?

java - session 参数始终为空

java - 从 servlet 发出请求

java - JerseyTest 在 Jersey 2.13 中使用 GrizzlyWebTestContainerFactory

java - 生成动态内容时出错

apache-flex - 在 Flex 中,我可以发送 HTTPService POST 请求而不是 GET 请求吗?

http - CORS 使用带有 ARR 和 URL 重写的 IIS 作为反向代理