Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 如何控制来自同一POJO的JSON序列化结果?_Spring_Jackson - Fatal编程技术网

Spring 如何控制来自同一POJO的JSON序列化结果?

Spring 如何控制来自同一POJO的JSON序列化结果?,spring,jackson,Spring,Jackson,我有一个基于Spring Boot的应用程序,Jackson用于JSON序列化/反序列化,JodaTime用于LocalDate对象 我有一个用户类: public class User { private String firstname; private String lastname; private String email; private LocalDate birthday; // constructor, getter, sett

我有一个基于Spring Boot的应用程序,Jackson用于JSON序列化/反序列化,JodaTime用于LocalDate对象

我有一个用户类:

public class User {

    private String firstname;

    private String lastname;

    private String email;

    private LocalDate birthday;

    // constructor, getter, setter...
}
我有两个Web服务,它们不公开相同的字段

例如:

  • WS1将公开所有字段,并以“yyyy-MM-dd”格式翻译生日
  • WS2将只公开firstname、lastname和生日字段,并将生日转换为“yyyy/MM/dd”格式
它会给我这样的东西:

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序列化。但我不知道如何将每个Web服务定义良好的objectMapper传递给@JsonView

我不想创建两个DTO来代表我的用户类的每个视图。我想要一些可以通过注释配置的东西