【发布时间】:2016-11-05 14:19:01
【问题描述】:
由于我的两个 JPA 实体(任务和作业)的双向关系中存在嵌套循环,我收到了 Jackson 错误。我开始研究并部分设法使用@JsonManagedReference 和@JsonBackReference 注释来解决这个问题,但是这种方法仅在我在我的实体中使用注释时才有效,这以跳过我的 JobResource/JobResourceAssembler 和 TaskResource/TaskResourceAssembler 的 JSON 序列化结束,得到HATEOASless 和 HALless json 响应。
有没有办法让我的资源管理这个序列化/反序列化?
一些代码(这样我得到了一个 json 响应,但是交换了 HATEOASless 和 HALless):
@Entity
public class Task {
@Id
@GeneratedValue
private Long id;
@OneToMany(mappedBy="task")
@JsonManagedReference
private List<Job> job = new ArrayList<Job>();
//constructors, getter, setter...
@Entity
public class Job {
@Id
@GeneratedValue
private Long id;
@ManyToOne
@JsonBackReference
@JoinColumn(name = "task_id", updatable = true, insertable = true, nullable = true)
private Task task;
//constructor, setter, getter.
HATEOASless 响应(工作应该有链接)
{
"_embedded": {
"tasks": [
{
"name": "Task",
"description": "Task Description",
"createdAt": 1467583658749,
"updatedAt": null,
"deletedAt": null,
"isActive": true,
"estimatedStartDate": null,
"startDate": null,
"estimatedDateEnd": null,
"dateEnd": null,
"ids": 1,
"risk": null,
"job": [
{
"id": 2,
"name": "Job",
"description": "Job Description",
"createdAt": 1467583673859,
"updatedAt": null,
"deletedAt": null,
"isActive": true
},
{
"id": 3,
"name": "Job2",
"description": "Job Description",
"createdAt": 1467583676138,
"updatedAt": null,
"deletedAt": null,
"isActive": true
},
{
"id": 4,
"name": "Job3",
"description": "Job Description",
"createdAt": 1467583679339,
"updatedAt": null,
"deletedAt": null,
"isActive": true
}
],
"_links": {
"self": {
"href": "http://127.0.0.3:7890/api/v1/tasks/1"
},
"task": {
"href": "http://127.0.0.3:7890/api/v1/tasks/1"
}
}
}
]
【问题讨论】:
标签: java json spring jackson spring-hateoas