What's the Jackson deserialization equivalent of @JsonUnwrapped?

后端 未结 4 1175
天命终不由人
天命终不由人 2020-12-01 13:43

Say I have the following class:

public class Parent {
  public int age;
  @JsonUnwrapped
  public Name name;
}

Producing JSON:



        
4条回答
  •  时光说笑
    2020-12-01 14:03

    You can use @JsonCreator with @JsonProperty for each field:

    @JsonCreator
    public Parent(@JsonProperty("age") Integer age, @JsonProperty("firstName") String firstName,
            @JsonProperty("lastName") String lastName) {
        this.age = age;
        this.name = new Name(firstName, lastName);
    }
    

    Jackson does type checking and unknown field checking for you in this case.

提交回复
热议问题