首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >忽略Jackson OnDemand中的嵌套属性

忽略Jackson OnDemand中的嵌套属性
EN

Stack Overflow用户
提问于 2017-04-25 18:02:14
回答 2查看 4.4K关注 0票数 4

我正在开发一个spring引导应用程序,使用Hibernate作为ORM,Jackson作为JSON序列化器。

对于这三个模型,我有三个模型对象和CRUD操作。

代码语言:javascript
复制
Class Student{
     private Teacher teacher;  // Teacher of the student — to be fetched eagerly
    +Getter/Setter
}

class Teacher {
      private List<Subject> subject;  // List of subjects associated to that user— to be fetched eagerly
      +Getter/Setter 
}

class Subject {
     private long subjectId 
    //Other subject properties
    + Getter/Setter
}

每当我触发一个索取学生信息的请求时,我就会得到老师的信息,这是正确的,因为我也收到了主题信息,这对我来说是不必要的。同时,当我要求老师的信息,我需要的主题信息应该是与此相联系的。如果我用@JsonBackReference作为研究对象,我就会一直失去它。我不知道如何做到这一点。

提前感谢您的帮助!!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-04-26 09:43:46

您可以使用JSON观点

春季博客:

代码语言:javascript
复制
public class View {
  interface Summary {}
}

public class User {

  @JsonView(View.Summary.class)
  private Long id;

  @JsonView(View.Summary.class)
  private String firstname;

  @JsonView(View.Summary.class)
  private String lastname;

  private String email;
  private String address;
  private String postalCode;
  private String city;
  private String country;
}

public class Message {

  @JsonView(View.Summary.class)
  private Long id;

  @JsonView(View.Summary.class)
  private LocalDate created;

  @JsonView(View.Summary.class)
  private String title;

  @JsonView(View.Summary.class)
  private User author;

  private List<User> recipients;

  private String body;
}

在控制器里

代码语言:javascript
复制
@RestController
public class MessageController {

  @Autowired
  private MessageService messageService;

  @JsonView(View.Summary.class)
  @RequestMapping("/")
  public List<Message> getAllMessages() {
    return messageService.getAll();
  }

  @RequestMapping("/{id}")
  public Message getMessage(@PathVariable Long id) {
    return messageService.get(id);
  }
}

PS:没有链接到http://fasterxml.com/,因为它目前处于关闭状态。

票数 3
EN

Stack Overflow用户

发布于 2018-09-27 14:23:16

您也可以这样注释

代码语言:javascript
复制
Class Student{
    @JsonIgnoreProperties("subject")
    private Teacher teacher;  // Teacher of the student — to be fetched eagerly
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43617923

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档