You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot注册接口报错:请求方法'GET'不支持如何解决?

问题:POST请求被识别为GET,抛出HttpRequestMethodNotSupportedException

我正在学习Spring Boot、REST和MVC相关技术,编写了一个将用户信息注册到数据库的方法,使用@PostMapping("/add")注解,前端HTML表单也设置了method="POST"请求方式。技术栈包含Spring Web、JPA Repository、Thymeleaf、H2数据库及Devtools。但浏览器发起的请求被识别为GET,控制台报错:

2023-03-13T16:04:50.044+01:00 WARN 38616 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' is not supported]

后端代码

@PostMapping("/add")
public ModelAndView addProfile(@ModelAttribute Profilo profilo){ //ModelAndView用于MVC模式,返回HTML页面
    // @ModelAttribute用于将HTTP请求参数映射到对应Java对象,和@RequestBody的区别是:RequestBody映射整个HTTP请求体到Java对象
    // 而ModelAttribute是将请求参数映射到对象属性

    Profilo find = repo.findByUsername(profilo.getUsername()); //调用根据用户名查找用户的方法
    if(!profilo.isValidPassword(profilo.getPassword())){
        ModelAndView modelAndView = new ModelAndView("profile"); //关联模型数据和前端视图的类
        modelAndView.addObject("error", "密码不符合要求,需满足:\n" +
                "至少8个字符\n" +
                "一个大写字母\n" +
                "一个小写字母\n" +
                "一个1-9的数字"); //addObject用于返回错误或成功消息
        return modelAndView;
    } else if (find != null) {
        ModelAndView modelAndView = new ModelAndView("profile");
        modelAndView.addObject("error", "该用户名已被使用,请更换");
        return modelAndView;
    }
    repo.save(profilo);
    ModelAndView modelAndView = new ModelAndView("profile");
    modelAndView.addObject("success", "用户注册成功");
    return modelAndView;
}

前端代码(位于templates文件夹)

<!DOCTYPE html>
<html>
<head>
  <title>用户注册表单</title>
</head>
<body>
<h1>用户注册表单</h1>
<form method="POST" action="/add">
  <label for="username">用户名:</label>
  <input type="text" name="username" id="username" required><br>

  <label for="password">密码:</label>
  <input type="password" name="password" id="password" required><br>

  <input type="submit" value="注册">
</form>
</body>
</html>
解决方案

以下是几种可能的原因及修复方式:

1. 避免直接访问接口路径

如果是直接在浏览器地址栏输入http://localhost:8080/add,浏览器会发起GET请求,而你的接口仅支持POST。必须通过点击页面上的「注册」按钮提交表单,触发POST请求。

2. 补全Thymeleaf的CSRF令牌支持

Spring Boot默认启用CSRF防护,Thymeleaf表单需要自动生成CSRF令牌才能正常提交POST请求。修改前端代码,添加Thymeleaf命名空间并使用其路径表达式:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
  <title>用户注册表单</title>
</head>
<body>
<h1>用户注册表单</h1>
<form th:action="@{/add}" method="POST">
  <label for="username">用户名:</label>
  <input type="text" name="username" id="username" required><br>

  <label for="password">密码:</label>
  <input type="password" name="password" id="password" required><br>

  <input type="submit" value="注册">
</form>
</body>
</html>

Thymeleaf会自动在表单中插入隐藏的CSRF令牌输入框,解决因CSRF验证失败导致的请求异常。

3. 确认控制器类注解正确

确保包含addProfile方法的类添加了@Controller注解(而非@RestController,因为@RestController会返回JSON数据而非视图页面),否则Spring无法识别这是一个MVC控制器,可能导致请求映射失效。

4. 检查请求路径匹配度

确认表单的action路径/add与控制器的@PostMapping("/add")完全一致,注意路径的大小写、前缀是否匹配(Spring请求映射默认区分大小写)。


内容的提问来源于stack exchange,提问作者Giorsano

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 03:05:11