相同字段的@JsonUnwrapped和@JsonCreate会导致问题

时间:2019-03-12 21:08:53

标签: java jackson

当我在嵌套字段上使用@JsonUnwrapped时:

public class Person{
    private int id;
    @JsonUnwrapped
    private Father father
    //getters/setters

    @Data
    @AllArgsConstructor
    private static class Father {
        private String name;
        private String surname;
   }

同时我使用@JsonCreator

@JsonCreator // DESERIALIZATION: JSON -> POJO
public Person(...
    @JsonProperty("name") String name,
    @JsonProperty("surname") String surname) {
    (...)
    this.father = new Father(name, surname);

其中Father是嵌套类。

我得到了错误:

Father` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creat

但是当我删除@JsonUnwrapped时,该字段可以反序列化,但是在序列化期间不会变平。 How to assure that Father field will be serialised and deserialised flatten at the same time?

编辑:

我粘贴完整的代码:

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class Person {
  private int id;
  private String firstName;
  private String lastName;
  private boolean active;
  private Address address;
  private String[] languages;
  @JsonIgnore private boolean isTheKing;
 @JsonUnwrapped // SERIALIZATIONL POJO -> JSON
  private Father father;

  @JsonCreator // DESERIALIZATION: JSON -> POJO
  public Student(
      @JsonProperty("id") int id,
      @JsonProperty("firstName") String firstName,
      @JsonProperty("lastName") String lastName,
      @JsonProperty("active") boolean active,
      @JsonProperty("address") Address address,
      @JsonProperty("languages") String[] languages,
      @JsonProperty("isTheKing") boolean isTheKing,
      @JsonProperty("name") String name,
      @JsonProperty("surname") String surname) {
    this.id = id;
    this.firstName = firstName;
    this.lastName = lastName;
    this.active = active;
    this.address = address;
    this.languages = languages;
    this.isTheKing = isTheKing;
    this.father = new Father(name, surname);
  }

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public String getFirstName() {
    return firstName;
  }

  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }

  public String getLastName() {
    return lastName;
  }

  public void setLastName(String lastName) {
    this.lastName = lastName;
  }

  public boolean isActive() {
    return active;
  }

  public void setActive(boolean active) {
    this.active = active;
  }

  public Address getAddress() {
    return address;
  }

  public void setAddress(Address address) {
    this.address = address;
  }

  public String[] getLanguages() {
    return languages;
  }

  public void setLanguages(String[] languages) {
    this.languages = languages;
  }

  public Father getFather() {
    return father;
  }

  public void setFather(Father father) {
    this.father = father;
  }

  @Data
  @AllArgsConstructor
  static class Father {
    private String name;
    private String surname;
  }
}

以下主要方法失败:

ObjectMapper mapper = new ObjectMapper();

    Person myStudent =
        mapper.readValue(new File("src/main/resources/data/rest/studentIN.json"), Person.class);
    System.out.println(myStudent);

有错误:

  

线程“主”中的异常   com.fasterxml.jackson.databind.exc.InvalidDefinitionException:无法   构造com.example.demo.Person$Father的实例   (不存在像默认构造一样的创建者):无法反序列化自   对象值(没有基于委托或属性的创建者)

我用龙目岛:

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.6</version>
    </dependency>

1 个答案:

答案 0 :(得分:1)

它应该适用于简单的POJO模型。 Father类应为public

import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class JsonApp {

    public static void main(String[] args) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        mapper.enable(SerializationFeature.INDENT_OUTPUT);

        Person.Father father = new Person.Father();
        father.setName("Wit");
        father.setSurname("Pil");

        Person person = new Person();
        person.setId(1909);
        person.setFather(father);

        String json = mapper.writeValueAsString(person);
        System.out.println(json);

        System.out.println(mapper.readValue(json, Person.class));
    }
}

class Person {

    private int id;

    @JsonUnwrapped
    private Father father;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Father getFather() {
        return father;
    }

    public void setFather(Father father) {
        this.father = father;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", father=" + father +
                '}';
    }

    static class Father {
        private String name;
        private String surname;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getSurname() {
            return surname;
        }

        public void setSurname(String surname) {
            this.surname = surname;
        }

        @Override
        public String toString() {
            return "Father{" +
                    "name='" + name + '\'' +
                    ", surname='" + surname + '\'' +
                    '}';
        }
    }
}

上面的代码显示:

{
  "id" : 1909,
  "name" : "Wit",
  "surname" : "Pil"
}
Person{id=1909, father=Father{name='Wit', surname='Pil'}}

2.6.7起,我已经对其进行了许多版本的测试,并且效果很好。