如何仅对具有 Spring RestController 的对象的子对象应用 @JsonView - 序列化?
How to Apply @JsonView only for children of a Object with Spring RestController - serialization?
我有这个案例:
public class Project{
private long id;
private List<User> users;
// other properties and getter setter etc
}
public class User{
@JsonView(MinimalUser.class)
private long id;
@JsonView(MinimalUser.class)
private String name;
private ComplexObject anything;
}
现在的 RestMethod:
@JsonView(MinimalUser.class)
@GetMapping("/client/{id}")
public List<Project> findProjectsByClientId(@PathVariable long id) {
return projectService.findProjectsByClientId(id);
}
这里我只想要一个用最少的 Users 对象初始化的项目,但是什么都不会初始化,因为我的项目中没有 "MinimalUser.class" JsonView,因此它没有被初始化。
我不想将 @JsonView 批注放在项目中的所有变量中,因为这太过分了。
如何告诉控制器只将@JsonView-Filtering/Serialization 应用到项目的用户(子)?
因为这个调用工作正常(我只得到必填字段):
@JsonView(MinimalUser.class)
@GetMapping("/minimal")
public List<User> findAllUsers() {
return userService.findAllUsers();
}
如果扩展 MinimalUser
视图并将这个新视图应用于整个项目,您可以获得以下结果:
public class MinimalProject extends MinimalUser {}
public class DetailedProject extends MinimalProject {}
// apply view to entire class
@JsonView(MinimalProject.class)
public class Project {
// keep existing fields as is
// limit view for project
@JsonView(DetailedProject.class)
private String details;
}
// test
ObjectMapper mapper = new ObjectMapper().disable(MapperFeature.DEFAULT_VIEW_INCLUSION);
User u = new User();
u.setId(1);
u.setName("John Doe");
u.setEmail("john@doe.com");
u.setDetails("some details");
System.out.println("user default view: " + mapper.writeValueAsString(u));
System.out.println("user minimal view: " + mapper.writerWithView(MinimalUser.class).writeValueAsString(u));
Project p = new Project();
p.setId(1000);
p.setName("MegaProject");
p.setUsers(Arrays.asList(u));
p.setDetails("Project details: worth M");
System.out.println("project default view: " + mapper.writeValueAsString(p));
System.out.println("project minimal view: " + mapper.writerWithView(MinimalProject.class).writeValueAsString(p));
输出:
user default view: {"id":1,"name":"John Doe","details":"some details","email":"john@doe.com"}
user minimal view: {"id":1,"name":"John Doe"}
project default view: {"id":1000,"users":[{"id":1,"name":"John Doe","details":"some details","email":"john@doe.com"}],"name":"MegaProject","details":"Project details: worth M"}
project minimal view: {"id":1000,"users":[{"id":1,"name":"John Doe"}],"name":"MegaProject"}
我有这个案例:
public class Project{
private long id;
private List<User> users;
// other properties and getter setter etc
}
public class User{
@JsonView(MinimalUser.class)
private long id;
@JsonView(MinimalUser.class)
private String name;
private ComplexObject anything;
}
现在的 RestMethod:
@JsonView(MinimalUser.class)
@GetMapping("/client/{id}")
public List<Project> findProjectsByClientId(@PathVariable long id) {
return projectService.findProjectsByClientId(id);
}
这里我只想要一个用最少的 Users 对象初始化的项目,但是什么都不会初始化,因为我的项目中没有 "MinimalUser.class" JsonView,因此它没有被初始化。
我不想将 @JsonView 批注放在项目中的所有变量中,因为这太过分了。
如何告诉控制器只将@JsonView-Filtering/Serialization 应用到项目的用户(子)?
因为这个调用工作正常(我只得到必填字段):
@JsonView(MinimalUser.class)
@GetMapping("/minimal")
public List<User> findAllUsers() {
return userService.findAllUsers();
}
如果扩展 MinimalUser
视图并将这个新视图应用于整个项目,您可以获得以下结果:
public class MinimalProject extends MinimalUser {}
public class DetailedProject extends MinimalProject {}
// apply view to entire class
@JsonView(MinimalProject.class)
public class Project {
// keep existing fields as is
// limit view for project
@JsonView(DetailedProject.class)
private String details;
}
// test
ObjectMapper mapper = new ObjectMapper().disable(MapperFeature.DEFAULT_VIEW_INCLUSION);
User u = new User();
u.setId(1);
u.setName("John Doe");
u.setEmail("john@doe.com");
u.setDetails("some details");
System.out.println("user default view: " + mapper.writeValueAsString(u));
System.out.println("user minimal view: " + mapper.writerWithView(MinimalUser.class).writeValueAsString(u));
Project p = new Project();
p.setId(1000);
p.setName("MegaProject");
p.setUsers(Arrays.asList(u));
p.setDetails("Project details: worth M");
System.out.println("project default view: " + mapper.writeValueAsString(p));
System.out.println("project minimal view: " + mapper.writerWithView(MinimalProject.class).writeValueAsString(p));
输出:
user default view: {"id":1,"name":"John Doe","details":"some details","email":"john@doe.com"}
user minimal view: {"id":1,"name":"John Doe"}
project default view: {"id":1000,"users":[{"id":1,"name":"John Doe","details":"some details","email":"john@doe.com"}],"name":"MegaProject","details":"Project details: worth M"}
project minimal view: {"id":1000,"users":[{"id":1,"name":"John Doe"}],"name":"MegaProject"}