英文:
Is there any annotation for Spring data JPA to not return the entire entity in JSON? (Rest API (SQL))
问题
{
"id": 1,
"name": "Will",
"character": {
"id": 1
}
}
英文:
Code: (It's simple)
@GetMapping("/{id}")
public Optional<Person> getOne(@PathVariable Long id){
return personRepository.findById(id);
}
Example:
{
"id": 1,
"name": "Will",
"character": {
"id": 1,
"name": "Batman",
"power": 100
}
}
Example of how I would like it:
(Only the entity id, without all fields.)
{
"id": 1,
"name": "Will",
"character": {
"id": 1
}
}
答案1
得分: 1
你可以将这些字段标记为实体中的 @Transient。这些字段既不会被持久化,也不会被序列化或反序列化。
英文:
For that you can mark those fields as @Transient in entity. Those fields will neither be persisted nor be serialized, de-serialized..
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论