我在@RestController
和@ResponseBody
中尝试了以下方法,但是,列表转移到屏幕上打印的损坏的json中。在下图中,可以看到userList有3个项目,包括字段id,name,mobile等。但是,在json中,最后两项变为"$ref" : "$[0].upperOpt[0].lowerOpt[12]"
结果:
修改
@Entity
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Long id;
private String name;
@Column(unique = true, nullable = false)
private String mobile;
private String email;
@Column(nullable = false)
private int state;
private Date creationTime;
private Date modificationTime;
@Version
private int version;
/**
* An operator user, and his managed companies.
*/
@ManyToMany(mappedBy = "belongers")
private List<CorporationInfo> managedCompany;
/**
* An corporation user, and his companies.
*/
@ManyToMany(mappedBy = "admins")
private List<CorporationInfo> myCompany;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "OptOpt", joinColumns = { @JoinColumn(name = "managedUserId") }, inverseJoinColumns = { @JoinColumn(name = "userId") })
private List<User> upperOpt;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "OptOpt", joinColumns = { @JoinColumn(name = "userId") }, inverseJoinColumns = { @JoinColumn(name = "managedUserId") })
private List<User> lowerOpt;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "UserRole", joinColumns = { @JoinColumn(name = "userId") }, inverseJoinColumns = { @JoinColumn(name = "roleId") })
private List<Role> roleList;
@PrePersist
public void prePersist() {
Date now = new Date();
creationTime = now;
modificationTime = now;
}
@PreUpdate
public void preUpdate() {
modificationTime = new Date();
}
// GETTERS AND SETTERS are left out...
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", mobile='" + mobile + '\'' +
", email='" + email + '\'' +
", state=" + state +
", creationTime=" + creationTime +
", modificationTime=" + modificationTime +
", version=" + version +
", managedCompany=" + managedCompany +
", myCompany=" + myCompany +
", roleList=" + roleList +
'}';
}
}
我省略了页面null相关代码以节省空间:
public class PageVto<T> {
public PageVto(Page page) {
this.page = page;
}
private Page page;
public Long getTotal() {
return page.getTotalElements();
}
public int getPageSize() {
return page.getSize();
}
public int getPageNum() {
return page.getNumber() +1;
}
public List<T> getData () {
return page.getContent();
}
public void setPage(Page page) {
this.page = page;
}
}