我有一个基于Spring Boot的应用程序,其中Jackson用于JSON序列化/去激活,JodaTime用于LocalDate对象。
我有一个User类:
public class User {
private String firstname;
private String lastname;
private String email;
private LocalDate birthday;
// constructor, getter, setter...
}
我有两个不公开相同字段的网络服务。
例如:
它会给我这样的东西:
public class View {
public interface Default {}
public interface All extends Default {}
}
public class User {
@JsonView(View.Default.class)
private String firstname;
@JsonView(View.Default.class)
private String lastname;
@JsonView(View.All.class)
private String email;
@JsonView(View.Default.class)
private LocalDate birthday;
}
@RestController
public class UserController {
@RequestMapping("/ws1/user")
@JsonView(View.All.class)
public User userSeenByWS1() {
return new User("john", "doe", "john.doe@unknown.com", LocalDate.now());
/*
Must return {"firstname":"john","lastname":"doe","email":"john.doe@unknown.com","birthday":"2017-07-27"}
*/
}
@RequestMapping("/ws2/user")
@JsonView(View.Default.class)
public User userSeenByWS2() {
return new User("john", "doe", "john.doe@unknown.com", LocalDate.now());
/*
Must return {"firstname":"john","lastname":"doe","birthday":"2017/07/27"}
*/
}
}
目前,我知道我可以使用@JsonView注释控制字段序列化,我可以创建两个不同的ObjectMapper来控制LocalDate序列化。但我不知道如何为每个webservice传递给@JsonView定义良好的objectMapper。
我不想创建代表我的User类的每个视图的2个DTO。我想要一些我可以通过注释配置的东西。