私はjsonレスポンスをキャプチャし、{"page": "1"、 "total": "2"、 "records": "15"、 "rows":{{"id":1、 "firstName": "John"、 "lastName": "Smith"、 "username": "john"、 "password": "21232f297a57a5a743894a0e4a801fc3"、 "role":null}、{"id":2、 "firstName" "Jane"、 "lastName": "Adams"、 "username": "jane"、 "password": "ee11cbb19052e40b07aac0ca060c23ee"、 "role":null}、ジャクソンのシリアル化中に、子オブジェクトがnullですか?
私のユーザーとロールのクラスは非常にシンプルなので、私は何か些細なことを見落とさなければならないと思う。
@Entity(name="user")
public class User implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String firstName;
private String lastName;
@Column(unique=true)
private String username;
private String password;
@JsonIgnore
@OneToMany(mappedBy="user", cascade={CascadeType.PERSIST, CascadeType.MERGE},fetch =FetchType.EAGER)
private List<Role> roles = new ArrayList<Role>();
public List<Role> getRoles() {
return roles;
}
public void setRoles(List<Role> roles) {
this.roles = roles;
}
@ManyToOne
private Role role;
public User() {}
public User(String username, String password, String firstName, String lastName, Role role) {
this.username = username;
this.password = password;
this.firstName = firstName;
this.lastName = lastName;
this.role = role;
}
public User(String username, String firstName, String lastName, Role role) {
this.username = username;
this.firstName = firstName;
this.lastName = lastName;
this.role = role;
}
public User(String username) {
this.username = username;
}
public Long getId() {
return id;
}
public void setId(Long 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 String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
}
@Entity(name="role")
public class Role implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@JsonIgnore
@ManyToOne
@JoinColumn (name = "user_id", nullable= true)
private User user;
private Integer role;
public Role() {}
public Role(Integer role) {
this.role = role;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Integer getRole() {
return role;
}
public void setRole(Integer role) {
this.role = role;
}
}
私は
も@JsonUnwrapped @ManyToOne民間の役割の役割を試してみました。
しかし、このケースでは私のjsonがまったく役割を持っていないので、それはさらに悪いことです。 { "ID":1、 "firstNameの": "ジョン"、 "lastNameの": "スミス"、 "ユーザ名": "ジョン"、 "パスワード": "21232f297a57a5a743894a0e4a801fc3"}、