【发布时间】:2016-12-22 20:51:38
【问题描述】:
我有一个实体
@Entity
public class Post {
@Id
private Long id;
@NotNull
private Date createdAt;
@NotNull
private String text;
@NotNull
private Date updatedAt;
@OneToMany
private List<Comment> comments;
}
和另一个实体
@Entity
public class Comment {
@Id
private Long id;
@NotNull
private Date createdAt;
@NotNull
private String text;
@NotNull
private Date updatedAt;
}
我有一个简单的控制器,它返回给定 id 的 post json
@RestController
@RequestMapping("/posts")
public class ProductDimensionsController {
@RequestMapping(method = RequestMethod.GET)
public Post getPost(@RequestParam(value = "id") String id) throws ApiException {
...
...
...
}
}
我正在回复:
{
"id": 401,
"createdAt": 1482364510614,
"updatedAt": 1482364510614,
"text": "abc",
}
我想关注:
{
"id": 401,
"createdAt": 1482364510614,
"updatedAt": 1482364510614,
"text": "abc",
"comments": [{
"id": 101,
"createdAt": 1482364510614,
"updatedAt": 1482364510614,
"text": "xyz",
}]
}
如何在 json 响应中包含关联的 OneToMany 实体?
【问题讨论】:
-
如果您需要更多说明,请在 cmets 中询问。我会在早上回复。
标签: java json spring spring-boot