当前位置: 首页 > 知识库问答 >
问题:

@JsonUn包装和@JsonCreate为同一字段会导致问题

韦俊英
2023-03-14

当我在嵌套字段上使用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` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creat

但是,当我删除@JsonUn包装时,该字段会反序列化,但在序列化期间不会变平。如何确保父亲字段将同时序列化和反序列化变平?

编辑:

我粘贴完整代码:

@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);

有错误:

线程maincom.fasterxml.jackson.databind.exc.的异常:无法构造com.example.demo.Person$父亲的实例(没有Creator,如默认构造,存在):无法从Object值反序列化(没有基于委托或属性的Creator)

我用龙目岛:

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

共有1个答案

田谦
2023-03-14

它应该适用于简单的POJO模型。父亲类应该是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以来,我已经测试了它的许多版本,它工作正常。

 类似资料:
  • 我有一门课,比如: 发动机等级如下 我想使用Jackson将Car对象转换为JSON,其结构如下 我已经尝试了本文提供的答案,但它对我不起作用,因为字段名和getter不同 我知道如果字段名是engine,我可以在engine上使用@jsonunwrpped。但在这种情况下该怎么办呢。

  • 方法process()不更改记录的字段(键)值。假设所有算子的并行度都是2,那么keyBy()at(2)是否也会导致网络洗牌呢?也许keyBy()at(2)由于密钥值不变而具有前向策略避免网络通信代价的效果? 太好了~

  • 我试图运行一个依赖于某些python3库的PySpark作业。我知道我可以在Spark Cluster上安装这些库,但是因为我正在为多个作业重用集群,所以我更愿意捆绑所有依赖项,并通过指令将它们传递给每个作业。 为此,我使用: 它有效地压缩了需要在根级别使用的包中的所有代码。 在my中,我可以导入依赖项 并将.zip添加到我的Spark上下文中 到目前为止还不错。 但出于某种原因,这将在星火星团的

  • 我有两个类:Person和PersonAdapter。Person是从wsdl生成的,不能更改。 PersonAdapter是Person的对象适配器,具有一些附加属性。类的对象提供给我的服务的客户端。我将所有JPA和JAXB注释添加到PersonAdapter类,因为我无法更改Person类。 有没有可能解决这个问题?

  • 问题内容: 我创建了一个隐藏的表单元素 而且我正在尝试在servlet中通过此行获取值(如我之前所做的那样): 但是我明白了(第33行是上面的行): java.lang.NumberFormatException:null java.lang.Integer.parseInt(未知源)java.lang.Integer.parseInt(未知源)web.objects.UploadImage.do