SpringBoot接收RequestBody数据时,参数大写接收不到数据以及解决办法
今天遇到一个问题,就在前台传递过来一个JSON字符串,后端接收这个字符串,做出处理。对于我们后端,一般就是创建一个对象,接收这个对象数据,然后,这个时候遇到了问题。。。。。
前端传递过来的JSON串
"customObject": {
"accountPeriod": "123123",
"Invoice": "1234123421341234",
"paymentcondition": "xx",
"profitcenter": "xxx",
"rmbAmount": "xxx",
"totalAccountSubject": "总账xx"
},
刚开始我创建的对象:
@Data
public class CustomObjectDTO {
private String accountPeriod;
private String Invoice;
private String paymentcondition;
private String profitcenter;
private String rmbAmount;
private String totalAccountSubject;
}
测试一波:

发现问题,怎么没有接收到Invoice字段。
通过查阅资料发现,因为实体类属性字段要是大写开头,就必须添加@JsonProperty()注解才可以。
更改实体类:
@Data
public class CustomObjectDTO {
private String accountPeriod;
@JsonProperty("Invoice")
private String invoice;
private String paymentcondition;
private String profitcenter;
private String rmbAmount;
private String totalAccountSubject;
}
再次测试:

测试成功。。。
总结
SpringBoot接收RequestBody数据时,如果实体类的第一个字母大写,需要加上 @JsonProperty()注解才可以

本文介绍SpringBoot中接收RequestBody数据时,实体类字段首字母大写导致无法正确获取的问题及解决方案。通过使用@JsonProperty注解匹配JSON字段名,确保前后端数据交互正常。





