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
Alternative to this use following one statement:
setAnnotationIntrospector(introspector);
instead following two statements,
super.getDeserializationConfig().setAnnotationIntrospector(introspector);
super.getSerializationConfig().setAnnotationIntrospector(introspector);
It seems that the method withAnnotiationIntrospector
does not set AnnotiationIntrospector
. It's returns new DeserializationConfig/SerializationConfig
object instead (with correct AnnotiationIntrospector
).
So, my version of JaxbJacksonObjectMapper
:
public class JaxbJacksonObjectMapper extends ObjectMapper {
public JaxbJacksonObjectMapper() {
super();
final AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();
this.configure(org.codehaus.jackson.map.DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true);
this.configure(org.codehaus.jackson.map.SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
this.setDeserializationConfig(this.getDeserializationConfig().withAnnotationIntrospector(introspector));
this.setSerializationConfig(this.getSerializationConfig().withAnnotationIntrospector(introspector));
}
}
Now it support @XmlRootElement
, @XmlTransient
and other.
Wrap your "Issuers" in a generic holder-object. That's what I do when working with Jackson and Spring.
class JSONResponse {
private Object collection;
public JSONResponse(Object collection) {
this.collection = collection;
}
public Object getCollection() {
return collection;
}
}
...
@RequestMappin(...)
@ResponseBody
public Object someHandler(...) {
...
return new JSONResponse(issuers);
}
It seems that the spring configuration was not right. That did not override the messageconverters, so it was not taking the Mapper that I wrote.
To override those, the right spring config is:
<annotation-driven>
<message-converters>
<beans:bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<beans:property name="objectMapper" ref="customObjectMapper" />
</beans:bean>
</message-converters>
</annotation-driven>
Now I am getting the root element, but it is not taking the name given in the jabx annotation, but the name of the class.
It seems that the new method (withAnnotiationIntrospector) does not work as it should, or I am missing something. But if use the deprecated one, I get the proper root name defined in the JABX label.
public JaxbJacksonObjectMapper() {
final AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();
this.configure(org.codehaus.jackson.map.SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
this.configure(org.codehaus.jackson.map.DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true);
// When using the non deprecated method (withAnnotationIntrospector),
// there are problems with JAXB/JSON parser so don't use right now
super.getDeserializationConfig().setAnnotationIntrospector(introspector);
super.getSerializationConfig().setAnnotationIntrospector(introspector);
}