Spring Boot创建用户模块出现405 Method Not Allowed错误求助
问题解决步骤
错误根源分析
你遇到的405错误(Request method 'GET' is not supported)是因为:
- 点击「Add User」链接发起的是GET请求,但你的
UserController只配置了@PostMapping("/create")来处理POST请求,没有对应处理GET请求的方法,无法返回创建用户的表单页面。 - 原POST方法使用
@RequestBody是错误的:@RequestBody用于接收JSON格式的请求体,而表单提交的是application/x-www-form-urlencoded格式的数据,两者不匹配。 user_create.html中的th:field需要模型中有对应的绑定对象,否则会渲染失败。
具体修复方案
1. 修改UserController,添加GET方法并修正POST逻辑
package com.app.expensetracker.controller; import com.app.expensetracker.dto.UserCreateRequest; import com.app.expensetracker.entity.User; import com.app.expensetracker.service.UserService; import jakarta.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; import java.util.List; import java.util.Optional; @Controller @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @GetMapping("/user_list") public String getAllUsers(Model model) { model.addAttribute("user_list", userService.getAllUsers()); return "user"; } // 新增:处理GET请求,返回创建用户的表单页面 @GetMapping("/create") public String showCreateUserForm(Model model) { // 往模型中放入空的请求对象,用于Thymeleaf字段绑定 model.addAttribute("userCreateRequest", new UserCreateRequest()); return "user_create"; } // 修改:处理表单POST请求,接收表单数据 @PostMapping("/create") public String createUser(@ModelAttribute @Valid UserCreateRequest userCreateRequest) throws Exception { userService.createUser(userCreateRequest); // 创建成功后重定向到用户列表(遵循PRG模式,避免重复提交) return "redirect:/user/user_list"; } }
2. 调整user_create.html的表单绑定
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="ISO-8859-1"> <title>Add User</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> </head> <body> <br> <br> <div class = "container"> <div class = "row"> <div class="col-lg-6 col-md-6 col-sm-6 container justify-content-center card"> <h1 class = "text-center"> Create New User </h1> <div class = "card-body"> <!-- 使用Thymeleaf的th:action和th:object绑定模型对象 --> <form th:action="@{/user/create}" th:object="${userCreateRequest}" method="POST"> <div class="form-group"> <label> First Name </label> <input type = "text" name = "firstName" th:field = "*{firstName}" class = "form-control" placeholder="Enter User First Name" /> </div> <div class="form-group"> <label> Last Name </label> <input type = "text" name = "lastName" th:field = "*{lastName}" class = "form-control" placeholder="Enter User Last Name" /> </div> <div class = "box-footer"> <button type="submit" class = "btn btn-primary"> Submit </button> </div> </form> </div> </div> </div> </div> </body> </html>
修复说明
- 添加GET方法:
@GetMapping("/create")负责返回创建用户的表单页面,解决点击链接时的GET请求不被支持的问题。 - 替换@RequestBody为@ModelAttribute:
@ModelAttribute专门用于绑定表单提交的参数,适配表单数据格式。 - PRG模式重定向:创建成功后重定向到用户列表页面,避免用户刷新页面导致重复提交表单。
- Thymeleaf字段绑定:在GET方法中放入空的
UserCreateRequest对象,让th:field能正确渲染表单字段。
内容的提问来源于stack exchange,提问作者Giribala
相关产品推荐
相关产品推荐

