How to map POJO to several JSON presentation

前端 未结 1 967
误落风尘
误落风尘 2020-12-18 17:37

How to map POJO to several JSON presentation?

I am using Jackson.

I want something like code below

@JsonIgnorePropertiesStreamA({ \"value2\"         


        
相关标签:
1条回答
  • 2020-12-18 18:08

    You use JSON Views

    class Views {
        static class PublicView { }
        static class StreamA extends PublicView { }
        static class OtherWay extends PublicView { }
    }
    
    public class Value {
        @JsonView(Views.PublicView.class) public int value;
        @JsonView(Views.OtherWay.class) public int value2;
        @JsonView(Views.StreamA.class) public int value3;
    }
    
    
    String json = new ObjectMapper()
                  .writerWithView(Views.OtherWay.class)
                  .writeValueAsString(valueInstance);
    

    Note that these are inclusive rather than exclusive; you create a view that includes the fields you want.

    0 讨论(0)
提交回复
热议问题