I am having one issue with Spring MVC and its json support. I make one ajax call to get some data and I want to get that data in json format including the root value. I am also using JABX
annotations in the entities because those are used for some REST API
.
I have read that to get the root value included with Jackson
, I should use this method:
this.configure(org.codehaus.jackson.map.DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true);
So I created one objectMapper which extends the codehaus one and looks like this:
public class JaxbJacksonObjectMapper extends ObjectMapper {
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.WRAP_ROOT_VALUE, true);
super.getSerializationConfig().withAnnotationIntrospector(introspector);
}
}
For Spring to use this mapper I have configured the following lines:
<beans:bean id="customObjectMapper" class="com.test.package.config.JaxbJacksonObjectMapper" />
<beans:bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<beans:property name="objectMapper" ref="customObjectMapper" />
</beans:bean>
<beans:bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<beans:property name="messageConverters">
<beans:list>
<beans:ref bean="jsonMessageConverter"/>
</beans:list>
</beans:property>
</beans:bean>
And my entities look like this:
@XmlRootElement(name = "collection")
public class Issuers {
private List<Issuer> issuers;
}
The problem is that when Spring 3.1
returns the Issuers json object to the browser it does not include the collection
root element.
Any idea how to solve this issue?
Thanks!