我正在开发一个spring引导应用程序,使用Hibernate作为ORM,Jackson作为JSON序列化器。
对于这三个模型,我有三个模型对象和CRUD操作。
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作为研究对象,我就会一直失去它。我不知道如何做到这一点。
提前感谢您的帮助!!
发布于 2017-04-26 09:43:46
您可以使用JSON观点
春季博客:
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;
}在控制器里
@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/,因为它目前处于关闭状态。
发布于 2018-09-27 14:23:16
您也可以这样注释
Class Student{
@JsonIgnoreProperties("subject")
private Teacher teacher; // Teacher of the student — to be fetched eagerly
}https://stackoverflow.com/questions/43617923
复制相似问题