使用Thymeleaf提交更新客户表单时出现Required body is missing错误求助
问题原因
Thymeleaf传统表单提交的是application/x-www-form-urlencoded或multipart/form-data格式的表单数据,但@RequestBody注解要求请求体是JSON/XML这类序列化格式,Spring无法将表单数据解析为CustomerDTO对象,因此抛出"Required body is missing"错误。
解决方案
方案一:改用@ModelAttribute接收表单数据(推荐,适配传统表单场景)
修改控制器方法,将@RequestBody替换为@ModelAttribute,Spring会自动将表单参数绑定到CustomerDTO实例:
@RequestMapping(path = "updateCustomerForm/{identityNumber}") public String updateCustomerForm(@PathVariable("identityNumber") String identityNumber, @ModelAttribute CustomerDTO customer){ customerService.updateCustomer(identityNumber,customer); return "redirect:/customer/showList"; }
同时调整Thymeleaf表单:
- 浏览器原生不支持PUT请求,将表单
method改为POST,靠_method隐藏字段模拟PUT请求(Spring Boot默认已开启HiddenHttpMethodFilter支持该功能) - 修正action路径的变量写法,确保
identityNumber正确传入 - 移除重复的
identityNumber隐藏字段(th:field已自动生成对应input)
<form th:action="@{/customer/updateCustomerForm/{id}(id=${customer.identityNumber})}" th:object="${customer}" method="post"> <input name="_method" type="hidden" value="put" /> <input type="text" th:field="*{identityNumber}" placeholder="Enter identity number" class="form-control col-4 mb-4" /> <input type="text" th:field="*{name}" placeholder="Enter name" class="form-control col-4 mb-4" /> <input type="text" th:field="*{surname}" placeholder="Enter surname" class="form-control col-4 mb-4" /> <input type="text" th:field="*{phoneNumber}" placeholder="Enter phone number" class="form-control col-4 mb-4" /> <input type="text" th:field="*{salary}" placeholder="Enter salary" class="form-control col-4 mb-4" /> <input type="text" th:field="*{age}" placeholder="Enter age" class="form-control col-4 mb-4" /> <button class="btn btn-primary col-2" type="submit">Save</button> </form>
方案二:用AJAX提交JSON数据(适配@RequestBody场景)
如果必须保留@RequestBody,需要通过AJAX将表单数据转为JSON格式提交:
首先明确控制器的请求类型和Content-Type:
@RequestMapping(path = "updateCustomerForm/{identityNumber}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE) public String updateCustomerForm(@PathVariable("identityNumber") String identityNumber, @RequestBody CustomerDTO customer){ customerService.updateCustomer(identityNumber,customer); return "redirect:/customer/showList"; }
然后修改表单,用JavaScript处理提交逻辑:
<form th:object="${customer}" id="updateForm"> <input type="text" th:field="*{identityNumber}" placeholder="Enter identity number" class="form-control col-4 mb-4" /> <input type="text" th:field="*{name}" placeholder="Enter name" class="form-control col-4 mb-4" /> <input type="text" th:field="*{surname}" placeholder="Enter surname" class="form-control col-4 mb-4" /> <input type="text" th:field="*{phoneNumber}" placeholder="Enter phone number" class="form-control col-4 mb-4" /> <input type="text" th:field="*{salary}" placeholder="Enter salary" class="form-control col-4 mb-4" /> <input type="text" th:field="*{age}" placeholder="Enter age" class="form-control col-4 mb-4" /> <button class="btn btn-primary col-2" type="button" onclick="submitForm()">Save</button> </form> <script> function submitForm() { const form = document.getElementById('updateForm'); const formData = new FormData(form); const customer = {}; formData.forEach((value, key) => { customer[key] = value; }); const identityNumber = customer.identityNumber; fetch(`/customer/updateCustomerForm/${identityNumber}`, { method: 'PUT', headers: { 'Content-Type': 'application/json', 'Accept': 'text/html' }, body: JSON.stringify(customer) }).then(response => { if (response.ok) { window.location.href = '/customer/showList'; } }).catch(error => { console.error('Error:', error); }); } </script>
内容的提问来源于stack exchange,提问作者Ege Coskun
相关产品推荐
相关产品推荐

