我正在使用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
主题,我会一直迷路。我不确定如何实现这一目标。
在此先感谢您的帮助!!
您可以使用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/,因为它当前已关闭。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句