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

使用SpringBoot+Thymeleaf调用下游API提交表单遇400错误求助

问题分析与解决

收到的400错误明确提示appointmentDate和appointmentTime不能为空,核心原因是你发送给下游API的JSON参数中,这两个字段要么未正确传递,要么字段名不匹配。结合你的代码来看:

1. 字段名不匹配问题

你的HTML表单和实体类中使用的是appointmentStartTime,但下游API期望的字段是appointmentTime,这就导致下游无法识别appointmentStartTime,进而判定appointmentTime为空。

解决办法

方法一:统一字段名

  • 修改Appointment实体类,将appointmentStartTime字段改为appointmentTime
  • 同步更新HTML表单的th:field:
<label>appointmentTime:</label>
<input type="time" th:field="*{appointmentTime}"  step="1" /><br/>

方法二:用注解映射字段名

如果不想修改实体类字段名,在Appointment类的appointmentStartTime字段上添加@JsonProperty注解,指定序列化后的字段名:

import com.fasterxml.jackson.annotation.JsonProperty;

public class Appointment {
    private LocalDate appointmentDate;
    
    @JsonProperty("appointmentTime")
    private LocalTime appointmentStartTime;
    
    // 对应的getter、setter方法
}

2. 确认参数绑定是否正常

  • 检查Appointment实体类的字段类型:appointmentDate应为LocalDate,appointmentStartTime(或appointmentTime)应为LocalTime,Spring Boot默认支持这两种类型的表单参数绑定。
  • 在Controller中临时打印实体对象,确认表单值是否正确绑定:
@PostMapping("/showform")
@ResponseBody
public String submitForm(@ModelAttribute ("appointment") Appointment appointment) {
    System.out.println(appointment); // 打印参数确认绑定结果
    // 后续API调用代码...
}

3. 验证发送的JSON内容

通过日志或调试工具查看RestTemplate发送的JSON结构,确保appointmentDate和appointmentTime字段存在且有有效值。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 17:33:25