2017-08-24 153 views
1

我需要忽略我的Json对象中的某些属性基于自定义Ignore注释,我只需要一个特定的对象映射器。但是,对于其他情况,我需要JsonProperty注释。我正在使用JacksonAnnotationInstrospector来做到这一点。但是当我这样做时,没有任何注释得到承认。@JsonIgnore和@JsonProperty在相同的属性

举例来说,我希望我的输出是这样的:

普通对象映射:{"element3":"C","element_1":"A","element_2":"B"}

使用对象映射器与JacksonAnnotationInstrospector

预期输出:{"element_1":"A"}

输出其实我与JacksonAnnotationInstrospector获得: {"element2":"B","element_1":"A"}

以下是我的代码:

public class TestClass { 

    public static void main(String[] args) throws Exception { 

     ObjectMapper objectMapper = new ObjectMapper(); 
     objectMapper.setAnnotationIntrospector(new JsonIgnoreIntrospector()); 

     MockObject mockObject = new MockObject("A", "B", "C"); 
     String string = objectMapper.writeValueAsString(mockObject); 
     System.out.println(string); 
    } 

    public static class MockObject { 

     @JsonProperty("element_1") 
     String element1; 

     @CustomJsonIgnore 
     @JsonProperty("element_2") 
     String element2; 

     @CustomJsonIgnore 
     String element3; 

     public MockObject(String element1, String element2, String element3) { 
      this.element1 = element1; 
      this.element2 = element2; 
      this.element3 = element3; 
     } 

     public String getElement1() { 
      return element1; 
     } 

     public void setElement1(String element1) { 
      this.element1 = element1; 
     } 

     public String getElement2() { 
      return element2; 
     } 

     public void setElement2(String element2) { 
      this.element2 = element2; 
     } 

     public String getElement3() { 
      return element3; 
     } 

     public void setElement3(String element3) { 
      this.element3 = element3; 
     } 
    } 

    public static class JsonIgnoreIntrospector extends JacksonAnnotationIntrospector { 
     @Override 
     public boolean hasIgnoreMarker(final AnnotatedMember m) { 
      return m.hasAnnotation(CustomJsonIgnore.class) || m.hasAnnotation(JsonIgnore.class); 
     } 

    } 

    @Retention(RetentionPolicy.RUNTIME) 
    @Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.LOCAL_VARIABLE, ElementType.CONSTRUCTOR}) 
    @JacksonAnnotation 
    public @interface CustomJsonIgnore { 
    } 
} 

回答

0

您应该在getter上而不是在字段上使用注释。并且在注解目标中添加METHOD。

工作液:

public class TestClass { 

    public static void main(String[] args) throws Exception { 

     ObjectMapper objectMapper = new ObjectMapper(); 
     objectMapper.setAnnotationIntrospector(new JsonIgnoreIntrospector()); 

     MockObject mockObject = new MockObject("A", "B", "C"); 
     String string = objectMapper.writeValueAsString(mockObject); 
     System.out.println(string); 
    } 

    public static class MockObject { 

     String element1; 

     String element2; 

     String element3; 

     public MockObject(String element1, String element2, String element3) { 
      this.element1 = element1; 
      this.element2 = element2; 
      this.element3 = element3; 
     } 

     @JsonProperty("element_1") 
     public String getElement1() { 
      return element1; 
     } 

     public void setElement1(String element1) { 
      this.element1 = element1; 
     } 

     @CustomJsonIgnore 
     @JsonProperty("element_2") 
     public String getElement2() { 
      return element2; 
     } 

     public void setElement2(String element2) { 
      this.element2 = element2; 
     } 

     @CustomJsonIgnore 
     public String getElement3() { 
      return element3; 
     } 

     public void setElement3(String element3) { 
      this.element3 = element3; 
     } 
    } 

    public static class JsonIgnoreIntrospector extends JacksonAnnotationIntrospector { 
     @Override 
     public boolean hasIgnoreMarker(final AnnotatedMember m) { 
      return m.hasAnnotation(CustomJsonIgnore.class) || m.hasAnnotation(JsonIgnore.class); 
     } 

    } 

    @Retention(RetentionPolicy.RUNTIME) 
    @Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.LOCAL_VARIABLE, 
      ElementType.CONSTRUCTOR }) 
    @JacksonAnnotation 
    public @interface CustomJsonIgnore { 
    } 
} 
相关问题