javaspringspring-bootjunitobjectmapper

How can i ignore @JsonIgnoreProperties in ObjectMapper


I have following class in my Java application:

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

    private String myProperty;

// getter and setter 
}

I want convert this object to json with ObjectMapper, but 'myProperty' does not convert and i want ignore that annotation(@JsonIgnoreProperties) but remember that i want still use another annotation. before ask question i try fix it and define kind of custom Introspector and setAnnotationIntrospector to my object mapper separately and test it but problem not resolve:

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);
        }
    }
}

another Introspector :

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);
        }
    }
}

I want following result:

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

can any one help me?


Solution

  • TestCustomIntrospector2 :

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