java - 同一属性上的 @JsonIgnore 和 @JsonProperty

标签 java json jackson-databind

我需要根据自定义忽略注释忽略 Json 对象中的某些属性,我只需要某个对象映射器。但是,对于其他情况,我需要 JsonProperty 注释。我正在使用 JacksonAnnotationIntrospector 来执行此操作。但当我这样做时,两个注释都没有得到确认。

例如,我希望我的输出是这样的:

普通对象映射器:{"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 {
    }
}

最佳答案

您应该在 getters 而不是字段上使用注释。并在注释目标中添加 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 {
    }
}

关于java - 同一属性上的 @JsonIgnore 和 @JsonProperty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45855970/

相关文章:

java - 通过jsp调用存储过程时出错

java - 在Java中使用正则表达式提取双引号之间的子字符串

java - hibernate 。检查日期范围是否在另一个日期范围内

javascript - 具有 API 后端数据预加载的单页应用程序

json - 使用 go-jsonnet 返回纯 JSON

java - 如何序列化在类级别提供的值 JsonInclude.Include.NON_NULL

java - 为什么这个正则表达式不匹配

c# - 忽略 Json.net 中的空字段

java - Jackson - 将 json 反序列化为通用类型

spring-boot - Spring Batch ChunkRequest 抛出 stackOverflow