spring-boot - JsonView ignore all but annotated properties
问题描述
I have the following JsonView
configuration:
public class JsonViews {
public static class AdminView {
}
public static class PublicView extends AdminView {
}
}
I have the following entity:
public class UserEntity {
@JsonView(JsonViews.AdminView.class)
private int id;
@JsonView(JsonViews.PublicView.class)
private int name;
// getters / setters
}
In my controller I have the following methods:
I want this method to return ALL properties
@JsonView(JsonViews.AdminView.class) public List<User> getAllOnlyForAdmin { return foo; }
I want this to return ONLY the name property
@JsonView(JsonViews.PublicView.class) public List<User> getAllOnlyForAdmin { return foo; }
Possible? If not, is there another solution?
解决方案
如果您对仅名称案例只有一个视图,则可能是这样;
public class JsonViews {
public static class NameView {
}
}
及该实体;
public class UserEntity {
private int id;
@JsonView(JsonViews.NameView.class)
private int name;
// getters / setters
}
和控制器方法;
@JsonView(JsonViews.NameView.class)
@RequestMapping("/admin/users")
public List<User> getUsersWithOnlyName() {
return userGetter.getUsers();
}
将为您提供每个名称字段User
,并且
@RequestMapping("/users")
public List<User> getUsers() {
return userGetter.getUsers();
}
将为您提供整个实体,即默认行为。
更多关于@JsonView
这里Spring
的主题6. Using JSON Views with Spring,也在spring.io
推荐阅读
- powerbi - 添加另一列时名称分解的计数或 ID
- javascript - 有哪些用于 AP Comp 的 JavaScript 抽象的好例子。科学。原则?
- sql - SQL 根据条件仅选择一行以获得两行上的完整状态
- php - 为什么我必须为 phpmailer 使用 include_path 目录?
- windows - 批处理 + 文件 + 一年中的某一天
- java - 尝试在空对象引用上调用虚拟方法 'void de.hdodenhof.circleimageview.CircleImageView.setOnClickListener...'
- javascript - 我将如何阻止或欺骗模糊事件侦听器?
- rest - 从批处理调用 REST API
- javascript - 为每个用户划分数量
- angularjs - AngularJS:加载动画时间