本文介绍了相同字段的@JsonUnwrapped和@JsonCreate会导致问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When I use @JsonUnwrapped on nested field:

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?

But when I remove the @JsonUnwrapped the field gets deserialised ok but is not flatten during serialisation. 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);

有错误:

我用龙目岛:

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

推荐答案

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

It should work for simple POJO model. Father class should be 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以来,我已经对其进行了许多版本的测试,并且效果很好.

I've tested it for many version since 2.6.7 and it works fine.

这篇关于相同字段的@JsonUnwrapped和@JsonCreate会导致问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 03:50