junit 如何在ObjectMapper中忽略@JsonIgnoreProperties

eqqqjvef  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(119)

在我的Java应用程序中有以下类:

@JsonIgnoreProperties(value = {"myProperty"}, allowSetters = true)
public class Test {

    private String myProperty;

// getter and setter 
}

我想用ObjectMapper把这个对象转换成json,但是'myProperty'不转换,我想忽略那个annotation(@JsonIgnoreProperties),但是记住我仍然想使用另一个annotation。在问问题之前,我尝试修复它,并定义一种自定义的Introspector和setAnnotationIntrospector分别到我的对象Map器,并测试它,但问题没有解决:

public class TestCustomIntrospector1 extends JacksonAnnotationIntrospector {

    @Override
    public PropertyName findNameForSerialization(Annotated a) {
        if (a.getAnnotated().equals(JsonIgnoreProperties.class)) {
            return null;
        }
        return super.findNameForSerialization(a);
    }
}

public class TestMain  {

    public static void main(String[] args) {
        Test obj = new Test();
        obj.setMyProperty("this is for test");
        try {
            String json = new ObjectMapper()
                    .setAnnotationIntrospector(new TestCustomIntrospector1())
                    .writerWithDefaultPrettyPrinter().writeValueAsString(obj);
            System.out.println(json);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

另一个内省者:

public class TestCustomIntrospector2 extends JacksonAnnotationIntrospector {

    @Override
    public boolean isAnnotationBundle(Annotation ann) {
        if (ann.annotationType() == JsonIgnoreProperties.class) {
            return false;
        }
        return super.isAnnotationBundle(ann);
    }

    @Override
    public boolean hasIgnoreMarker(AnnotatedMember m) {
        JsonIgnoreProperties ignoreProperties = m.getAnnotation(JsonIgnoreProperties.class);
        if (ignoreProperties != null) {
            return ignoreProperties.ignoreUnknown();
        }
        return super.hasIgnoreMarker(m);
    }
}

public class TestMain  {

    public static void main(String[] args) {
        Test obj = new Test();
        obj.setMyProperty("this is for test");
        try {
            String json = new ObjectMapper()
                    .setAnnotationIntrospector(new TestCustomIntrospector2())
                    .writerWithDefaultPrettyPrinter().writeValueAsString(obj);
            System.out.println(json);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

我想要以下结果:

{
  "myProperty": "this is for test"
}

有谁能帮我吗?

ktecyv1j

ktecyv1j1#

TestCustomIntrospector 2:

public class TestCustomIntrospector2 extends JacksonAnnotationIntrospector {

        @Override
        public JsonIgnoreProperties.Value findPropertyIgnoralByName(MapperConfig<?> config, Annotated a){
            return JsonIgnoreProperties.Value.empty();
        }
}
vvppvyoh

vvppvyoh2#

您可以使用以下命令使属性可用于序列化(json到object),但不可用于序列化(object到json):

allowSetters=true

如果你想将对象转换为json,你需要序列化该对象,然后你应该使用allowGetters=true:

@JsonIgnoreProperties(value = {"myProperty"}, allowGetters = true)
public class Test {
    private String myProperty;

    // getter and setter 
}

为了提高可读性:

public class Test {

    @JsonIgnoreProperties(allowGetters = true)
    private String myProperty;

    // getter and setter 
}

如果你想序列化和非序列化,你应该去掉注解。

相关问题