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

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

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

问题

  1. {
  2. "id": 1,
  3. "name": "Will",
  4. "character": {
  5. "id": 1
  6. }
  7. }
英文:

Code: (It's simple)

  1. @GetMapping("/{id}")
  2. public Optional<Person> getOne(@PathVariable Long id){
  3. return personRepository.findById(id);
  4. }

Example:

  1. {
  2. "id": 1,
  3. "name": "Will",
  4. "character": {
  5. "id": 1,
  6. "name": "Batman",
  7. "power": 100
  8. }
  9. }

Example of how I would like it:
(Only the entity id, without all fields.)

  1. {
  2. "id": 1,
  3. "name": "Will",
  4. "character": {
  5. "id": 1
  6. }
  7. }

答案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:

确定