我有以下映射:
1
public class Content {
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "content_id", unique = true, nullable = false)
private Long contentId;
@OneToOne(fetch = FetchType.LAZY, mappedBy = "content", cascade = CascadeType.ALL)
UserContent userContent;
....
}
<小时/>
@Entity
@Table(name="user_content")
public class UserContent {
@GenericGenerator(name = "generator", strategy = "foreign", parameters = @Parameter(name = "property", value = "content"))
@Id
@GeneratedValue(generator = "generator")
@Column(name = "content_id", unique = true, nullable = false)
private Long contentId;
@OneToOne(fetch = FetchType.EAGER)
@PrimaryKeyJoinColumn
Content content;
}
以及以下 dao:
public Set<UserContent> getAllContent() {
Session session = sessionFactory.getCurrentSession();
return new HashSet<UserContent>(session.createCriteria(UserContent.class).list());
}
当在 Controller 方法中返回此 dao 的结果时,我看到无限递归 json:
[{"contentId":1,"name":"DSC_0029.JPG","moderationStatus":"IN_PROGRESS","moderateComment":"","content":{"userContent":{"contentId":1,"name":"DSC_0029.JPG","moderationStatus":"IN_PROGRESS","moderateComment":"","content":{"userContent":{"contentId":1,"name":"DSC_0029.JPG","moderationStatus":"IN_PROGRESS","moderateComment":"","content":{"userContent":{"contentId":1,"name":"DSC_0029.JPG","moderationStatus":"IN_PROGRESS","moderateComment":"","content":{"userContent":{"contentId":1,"name":"DSC_0029.JPG","moderationStatus":"IN_PROGRESS","moderateComment":"","content":{"userContent":{"contentId":1,"name":"DSC_0029.JPG","moderationStatus":"IN_PROGRESS","moderateComment":"","content":{"userContent":....
2
我尝试像这样更改映射:
import com.fasterxml.jackson.annotation.JsonIgnore;
public class Content {
...
@OneToOne(fetch = FetchType.LAZY, mappedBy = "content", cascade = CascadeType.ALL)
@JsonIgnore //added this annotation
UserContent userContent;
....
结果:
HTTP Status 500 - Could not write JSON: failed to lazily initialize a collection of role: com.terminal.domain.TerminalUser.companys, no session or session was closed (through reference chain: java.util.HashSet[0]->com.terminal.domain.UserContent["user"]->com.terminal.domain.TerminalUser["companys"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: com.terminal.domain.TerminalUser.companys, no session or session was closed (through reference chain: java.util.HashSet[0]->com.terminal.domain.UserContent["user"]->com.terminal.domain.TerminalUser["companys"])
附注
我已阅读以下热门答案,但我无法解决我的问题 - 请帮助。
Infinite Recursion with Jackson JSON and Hibernate JPA issue
最佳答案
自 Jackson 2.0 起,您可以使用 @JsonIdentityInfo
。
检查documentation
已更新
...
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id")
public class Content {
...
}
...
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id")
public class UserContent {
...
}
关于java - spring mvc Controller 中的无限json序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27201791/