【问题标题】:HTTP Put method not working from Spring BootHTTP Put 方法在 Spring Boot 中不起作用
【发布时间】:2019-05-01 05:56:30
【问题描述】:

我正在使用 Spring Boot 编写一个简单的控制器。我正在尝试 Postman 的 Put、Post 方法。

@RequestMapping(path="/formData", method=RequestMethod.POST)
public String processPostFormData() {       
    return "practice/PutPage";
}

@RequestMapping(path="/formData", method=RequestMethod.PUT)
public String processPutFormData() {        
    return "practice/PutPage";
}

Post 按预期工作,但 Put 给了我以下响应正文:

“状态”:405, "error": "方法不允许", "message": "JSP 只允许 GET POST 或 HEAD"

另外,csrf().disable() 设置在 WebSecurityConfigurerAdapter 的子类中。

如何解决这个问题?

【问题讨论】:

  • 您是否同时在同一个地址进行 POST 和 PUT?看到方法名称也一样,这可能不是这种情况吗?
  • @Clomez 相同的方法名是错字;编辑它。我正在使用邮递员;因此,只能发送一种方法。不同的方法不能有相同的路径吗?
  • stackoverflow.com/questions/23886941/… 似乎是同样的问题

标签: spring-boot spring-security httprequest


【解决方案1】:

你可以只保留method="post",但隐藏你可以发送_methodput,它就像一个魅力。我个人将它用于spring框架。

<form method="post" ...>
  <input type="hidden" name="_method" value="put" />
...

更多信息请参考this

【讨论】:

  • 我正在使用邮递员进行锻炼。另外,虽然可以设置 _method 参数,但是这里不能设置 type=hidden。并且仅使用 _method=put 参数对,结果仍然相同。
猜你喜欢
  • 2019-10-23
  • 2020-10-08
  • 1970-01-01
  • 2021-03-05
  • 2021-10-05
  • 1970-01-01
  • 2019-04-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多