Spring Data JPA嵌入式实体类如何在Postman中传递请求体?
问题分析与解决方案
问题根源
你的JSON请求体存在两个核心错误,导致defectiveComponentInfo字段始终为null:
- 字段名大小写不匹配:实体类中嵌入字段的名称是
defectiveComponentInfo(小驼峰命名),但你JSON里写的是DefectiveComponentInfo(首字母大写),Jackson默认属性映射区分大小写,无法识别。 - 数据类型不匹配:实体类中
defectiveComponentInfo是单个DefectiveComponentInfo对象,而你用数组[]包裹了内部字段,类型不匹配导致反序列化失败。
正确的JSON请求体
{ "userName": "sam", "contactNumber": "96014587555", "defectiveComponentInfo": { "componentType": "Integral", "componentName": "Bummper", "quantity": 5, "description": "Repair product" } }
扩展场景提示
如果业务需要支持多个缺陷组件信息,需先修改实体类字段类型为集合:
@ElementCollection private List<DefectiveComponentInfo> defectiveComponentInfoList;
对应的JSON请求体就可以用数组形式:
{ "userName": "sam", "contactNumber": "96014587555", "defectiveComponentInfoList": [ { "componentType": "Integral", "componentName": "Bummper", "quantity": 5, "description": "Repair product" } ] }
内容的提问来源于stack exchange,提问作者vishwa_001
相关产品推荐
相关产品推荐

