I have a question regarding Json serialization / deserialization in Spring Boot.
I have an entity class, what has embedded id:
@Entity
@Table(name="USER_TABLE")
@Getter
@Setter
public class User {
private UserPK id;
private String name;
}
/* Getters and Setters excluded */
public class UserPK implements Serializable {
private Long userId;
private Long personalId;
}
Now if I serialize an object, Spring Boot put a root "id" tag inside JSON. Is it possible to exclude this, and put it into the same level of the name property?
Actual result:
{
"id":{"userId":321, "personalId":222},
"name":"John Doe"
}
Expected result:
{
"userId":321,
"personalId":222,
"name":"John Doe"
}
Thank you in advance!
CodePudding user response:
You may annotate User.id field with @JsonUnwrapped annotation to effectively flatten the underlying structure like this:
public class User {
@JsonUnwrapped
private UserPK id;
private String name;
}
See the docs.
