Search code examples
javajsonjackson

How to ignore pojo annotations while using Jackson ObjectMapper?


I've a POJO with Jackson annotations

     public class Sample{

        private String property1;

        @JsonIgnore
        private String property2;

        //...setters getters

     }

So, when Jackson library is used for automarshalling by other frameworks such as RestEasy these annotations help guide the serialization and deserilization process.

But when I want to explicitly serialize using ObjectMapper mapper = new ObjectMapper(), I don't want those annotations to make any effect, instead I will configure the mapper object to my requirement.

So, how to make the annotations not make any effect while using ObjectMapper?


Solution

  • Configure your ObjectMapper not to use those Annotations, like this:

    ObjectMapper objectMapper = new ObjectMapper().configure(
                     org.codehaus.jackson.map.DeserializationConfig.Feature.USE_ANNOTATIONS, false)
                        .configure(org.codehaus.jackson.map.SerializationConfig.Feature.USE_ANNOTATIONS, false);
    

    This works!