我有 Angular 发送的 JSON 字符串:{ "transaction_id": "1234", "usage": "Test Usage", "billing_address": { "first_name": "name", "last_name": "name", "address1": "street 1234", "zip_code": "11923" }, "shipping_address": { "first_name": "name", "last_name": "name", "address1": "street 1234", "zip_code": "11923" } }Java代码:public class DTO { private String transaction_id; private String usage; private BillingAddress billingAddress; private ShippingAddress shippingAddress; ... getter/setter}public class BillingAddress { private String firstName; private String lastName; private String address1; private String zip_code; ... getter/setter}public class ShippingAddress { private String firstName; private String lastName; private String address1; private String zip_code; ... getter/setter}弹簧端点:@PostMapping(value = "/{id}", consumes = { MediaType.APPLICATION_JSON_VALUE }, produces = { MediaType.APPLICATION_JSON_VALUE }) public ResponseEntity<?> handleWpfMessage(@PathVariable("id") id, @RequestBody DTO data){ ....}映射内部对象billing_address以及shipping_address正确映射值的正确方法是什么?我是否需要添加注释才能正确映射它们?
3 回答
慕沐林林
TA贡献2016条经验 获得超9个赞
您应该将以下注释添加到 DTO 类中:
public class DTO {
private String transaction_id;
private String usage;
@JsonProperty("billing_address")
private BillingAddress billingAddress;
@JsonProperty("shipping_address")
private ShippingAddress shippingAddress;
... getter/setter
}
哆啦的时光机
TA贡献1779条经验 获得超6个赞
您的角度客户端使用蛇盒。为了使 jackson 正确反序列化,您可以使用以下命令进行全局配置:
spring.jackson.property-naming-strategy=SNAKE_CASE
但是您也可以为特定的类配置它:
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class) public class DTO { }
撒科打诨
TA贡献1934条经验 获得超2个赞
正如已经提到的,您可以在类中使用 Jackson 属性映射注释DTO
。
@JsonProperty("billing_address") private BillingAddress billingAddress;
这意味着,在 json 中,属性billing_address
将被分配给billingAddress
变量。
添加回答
举报
0/150
提交
取消