如何在Spring Boot中将单个实体映射到请求体中的两个不同节点。

huangapple go评论103阅读模式
英文:

How to map single entity with two different node in requestbody in springboot

问题

public class Address {
    private String firstName;
    private String lastName;
    private String addressLne1;
    private String addressLne2;
    private String city;
    private String province;
    private String postalCode;
    private String country;
}

public class User {
    private Address billingAddress;
    private Address shippingAddress;
}

@RestController
public class Controller {
    @PostMapping("/user")
    public String user(@RequestBody User request) {
        return "success";
    }
}
{
  "data": {
    "billing_information": {
      "billing_first_name": "John",
      "billing_last_name": "Doe",
      "billing_address_line1": "1295 Charleston Road",
      "billing_address_line2": "Apt 1",
      "billing_city": "Mountain View",
      "billing_province": "CA",
      "billing_postal_code": "94043",
      "billing_country": "US"
    },
    "shipping_information": {
      "shipping_first_name": "John",
      "shipping_last_name": "Doe",
      "shipping_address_line1": "1295 Charleston Road",
      "shipping_address_line2": "Apt 1",
      "shipping_city": "Mountain View",
      "shipping_province": "CA",
      "shipping_country": "US",
      "shipping_postal_code": "94043"
    }
  }
}

在你的 Spring Boot 应用中,你可以按照上述代码的结构来定义 AddressUser 类,然后在 Controller 类中使用 @PostMapping 注解来处理 POST 请求,并使用 @RequestBody 注解将请求体映射到 User 对象。

英文:

I am using springboot app and I want to map request body User having fields billingAddress and shippingAddress. I have define single Address class given below. Request body is given below. How would I define mapper so that request body can be easily transform? I don't want to define separate billingAddress and shippingAddress class.

Json Request:

{
  "data": {
    "billing_information": {
      "billing_first_name": "John",
      "billing_last_name": "Doe",
      "billing_address_line1": "1295 Charleston Road",
      "billing_address_line2": "Apt 1",
      "billing_city": "Mountain View",
      "billing_province": "CA",
      "billing_postal_code": "94043",
      "billing_country": "US"
    },
    "shipping_information": {
      "shipping_first_name": "John",
      "shipping_last_name": "Doe",
      "shipping_address_line1": "1295 Charleston Road",
      "shipping_address_line2": "Apt 1",
      "shipping_city": "Mountain View",
      "shipping_province": "CA",
      "shipping_country": "US",
      "shipping_postal_code": "94043"
    }
  }
}

Address.java :

public class Address {
	private String firstName;
	private String lastName;
	private String addressLne1;
	private String addressLne2;
	private String city;
	private String province;
	private String postalCode;
	private String country;
}

User.java :

public class User {
	private Address billingAddress;
	private Address shippingAddress;
}

Controller.java :

@RestController
public class Controller {
    @PostMapping("/user")
    public String user(@RequestBody User request) {

        return "success";
    }
 }

答案1

得分: 1

首先,JSON 的根节点具有属性 'data',可以使用 @JsonTypeName 进行映射,另一种替代方法是创建一个包装类并使用 @JsonProperty 将其映射到映射器字段。

然后,可以使用 @JsonProperty 注解映射账单地址和送货地址。

@JsonTypeName("data")
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
class User {

    @JsonProperty("billing_information")
    private Address billingAddress;

    @JsonProperty("shipping_information")
    private Address shippingAddress;
    
    // 获取器和设置器
}

最后,由于 Address 模型字段需要映射到两个不同的 JSON 属性,我们可以使用 @JsonAlias 设置别名。

class Address {

    @JsonProperty("billing_first_name")
    @JsonAlias({"shipping_first_name"})
    private String firstName;

    @JsonProperty("billing_last_name")
    @JsonAlias({"shipping_last_name"})
    private String lastName;

    @JsonProperty("billing_address_line1")
    @JsonAlias({"shipping_address_line1"})
    private String addressLine1;

    ...

    // 获取器和设置器
}
英文:

First, the root node of a JSON has a property 'data', you can map using @JsonTypeName, other alternative is to create a wrapper class and map it using @JsonProperty to a mapper field

Then you can map the billing address and shipping address using @JsonProperty annotation

@JsonTypeName("data")
@JsonTypeInfo(include= JsonTypeInfo.As.WRAPPER_OBJECT,use= JsonTypeInfo.Id.NAME)
class User {

    @JsonProperty("billing_information")
    private Address billingAddress;

    @JsonProperty("shipping_information")
    private Address shippingAddress;
    
    // getters and setters
}

And finally, as Address model fields need to be mapped to 2 different JSON properties, we can use @JsonAlias to set an alias.

class Address {

    @JsonProperty("billing_first_name")
    @JsonAlias({"shipping_first_name"})
    private String firstName;

    @JsonProperty("billing_last_name")
    @JsonAlias({"shipping_last_name"})
    private String lastName;

    @JsonProperty("billing_address_line1")
    @JsonAlias({"shipping_address_line1"})
    private String addressLne1;

    ...

    // getters and setters
}

huangapple
  • 本文由 发表于 2020年9月20日 13:59:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63975981.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定