【问题标题】:Spring boot "Request method 'POST' not supported"Spring boot“不支持请求方法'POST'”
【发布时间】:2018-06-03 06:41:15
【问题描述】:
@Controller
public class ControllerClass {

    @RequestMapping(value="/test",method=RequestMethod.POST)
    public String test(){
        return "index";
    }

}

属性:

spring.mvc.view.prefix=/
spring.mvc.view.suffix=.html

这是一个简单的 mvc 控制器类,我在其中发布请求以获取资源/静态文件夹中的 index.html 页面。每当我从 REST 客户端或 html 表单发送发布请求时,我都会收到错误消息 - “不支持请求方法 'POST'”。 当我将“RequestMethod.POST”更改为“RequestMethod.GET”时,它工作正常。

【问题讨论】:

  • 您确定没有发送任何请求正文吗?
  • 您应该能够移除该控制器并且 GET 仍然可以工作。 spring boot 自动从 /resource/static 以及其他几个位置提供静态内容:spring.io/blog/2013/12/19/…。 POST 获取静态 html 文件真的没有意义,你必须 POST 吗?
  • 另外,@RestController 更适合您的方法的返回值预计会形成 HTTP 响应正文。作为一个简单的测试,将@ResponseBody 添加到您的test() 方法中。
  • 你启用 CSRF 了吗?如果是,那么您需要发送 csrf 令牌。
  • CSRF 未启用。

标签: java spring


【解决方案1】:

我通过在 pom.xml 中添加 thymeleaf stater 依赖项解决了这个问题,并将所有 html 文件从静态文件夹移动到模板文件夹。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

理想情况下,它应该在没有 thymeleaf 的情况下工作,但我不明白为什么它不支持 POST 请求。

【讨论】:

    猜你喜欢
    • 2020-03-31
    • 2016-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多