Showing posts with label annotation. Show all posts
Showing posts with label annotation. Show all posts

Wednesday, September 21, 2011

Jersey + Jackson: Serialize only annotated fields or methods with @JsonProperty

By default, when serializing objects, Jackson will auto-detect all public no-argument non-static getter methods and include them as properties in the final result (JSON string). I wanted Jackson to use only annotated methods and ignore rest of the methods of my object. To accomplish this we need to turn off the AUTO_DETECT_GETTERS feature in SerializationConfig object by configuring our ObjectMapper which holds an instance of SerializationConfig. I couldn't manage to inject a preconfigured SerializationConfig or ObjectMapper object. Then I found out that we need create an ObjectMapper provider class that implements ContextResolver, and annotate it with @Provider. This provider should then return an instance of ObjectMapper. This is where we will configure the object.