6

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.

spal
  • 731
  • 3
  • 9
  • 20
  • 1
    solved this issues with the help of this link http://stackoverflow.com/questions/9517189/spring-mvc-and-jackson-mapping-do-not-return-the-root-element-in-json – spal Oct 18 '12 at 20:31

1 Answers1

1

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);
user3763241
  • 111
  • 1
  • 6