gpt4 book ai didi

java - @JsonUnwrapped 和 @JsonCreate 对于同一字段会导致问题

转载 作者:行者123 更新时间:2023-11-30 05:44:12 27 4
gpt4 key购买 nike

当我在嵌套字段上使用@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 时,该字段可以正常反序列化,但在序列化过程中不会变平。如何保证Father字段同时被序列化和反序列化扁平化?

编辑:

我粘贴完整代码:

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

有错误:

Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of com.example.demo.Person$Father (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)

我使用 Lombok :

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

最佳答案

它应该适用于简单的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 以来,我已经对它的多个版本进行了测试,并且运行良好。

关于java - @JsonUnwrapped 和 @JsonCreate 对于同一字段会导致问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55130687/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com