简体   繁体   中英

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?

嗨,如果你使用com.fasterxml.jackson然后代码是 -

ObjectMapper mapper = new ObjectMapper().configure(MapperFeature.USE_ANNOTATIONS, true);

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM