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;
}
And at the same time I use the @JsonCreator:
@JsonCreator // DESERIALIZATION: JSON -> POJO
public Person(...
@JsonProperty("name") String name,
@JsonProperty("surname") String surname) {
(...)
this.father = new Father(name, surname);
with Father being nested class.
I get the error:
Father` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creat
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?
EDIT:
I paste full code:
@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;
}
}
the following main method fails:
ObjectMapper mapper = new ObjectMapper();
Person myStudent =
mapper.readValue(new File("src/main/resources/data/rest/studentIN.json"), Person.class);
System.out.println(myStudent);
with error:
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)
I use lombok:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
</dependency>
Jacksondo you use? WhyFatherclass is declared asprivate static class? It is not visible for other classes. - Michał Zioberpackage-scopeand the issue remains. I have also attached the lombok dependency version that I use. - mCs@NoArgsConstructorto yourfatherclass. - Michał Ziobernameandsurnameare nulls. Probably becouse the noarg constr is being called. So still no luck. - mCslombokannotations to see the result. In case it will be working we know thatlombokmess upPOJOclass. - Michał Ziober