spal
spal

Reputation: 771

@XmlJavaTypeAdapter not working with Jackson JSON

I am using Spring REST framework and Jackson JSON

<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
 <property name="objectMapper" ref="customObjectMapper" /> 
 <property name="supportedMediaTypes" value="application/json" />
</bean>
<bean id="customObjectMapper"class="com.rackspace.payment.webapp.utils.JaxbJacksonObjectMapper" />

public class JaxbJacksonObjectMapper extends ObjectMapper {
    @SuppressWarnings("deprecation")
    public JaxbJacksonObjectMapper() {
        final AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();

        this.configure(org.codehaus.jackson.map.DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true);
        super.getDeserializationConfig().withAnnotationIntrospector(introspector);

        this.configure(org.codehaus.jackson.map.SerializationConfig.Feature.WRITE_NULL_PROPERTIES, false);
        this.configure(org.codehaus.jackson.map.SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
        super.getSerializationConfig().withAnnotationIntrospector(introspector);
    }
}


//JXAB element
@XmlAttribute(name = "createDate")
@XmlJavaTypeAdapter(CustomDateAdapter.class)
protected Date createDate;

The issue I face is, when the JSON output is displayed 'createDate' element is displayed like a string the xml adapter class is not invoked, whereas it works fine in XML output.

What am I missing? how to invoke the XmlJavaTypeAdapter in JSON format.

Upvotes: 6

Views: 6672

Answers (1)

user3763241
user3763241

Reputation: 131

the code below did the trick for me .

    ObjectMapper objectMapper = new CustomObjectMapper(ENTITY_WITH_ID_DESERIALIZER);

    AnnotationIntrospector primary = new JacksonAnnotationIntrospector();
    AnnotationIntrospector secondary = new JaxbAnnotationIntrospector();
    AnnotationIntrospector pair = new AnnotationIntrospector.Pair(primary, secondary);

    objectMapper.setAnnotationIntrospector(pair);

    objectMapper.configure(DeserializationConfig.Feature.USE_ANNOTATIONS, true);

Upvotes: 3

Related Questions