Is there any annotation for Spring data JPA to not return the entire entity in JSON? (Rest API (SQL))

huangapple go评论117阅读模式
英文:

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..

huangapple
  • 本文由 发表于 2020年8月17日 23:53:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63454371.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定