spring-framework / org.springframework.http.converter.json / Jackson2ObjectMapperFactoryBean / <init>

<init>

Jackson2ObjectMapperFactoryBean()

A FactoryBean for creating a Jackson 2.x ObjectMapper (default) or XmlMapper (createXmlMapper property set to true) with setters to enable or disable Jackson features from within XML configuration.

It customizes Jackson defaults properties with the following ones:

Example usage with MappingJackson2HttpMessageConverter:

 <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="objectMapper"> <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean" p:autoDetectFields="false" p:autoDetectGettersSetters="false" p:annotationIntrospector-ref="jaxbAnnotationIntrospector" /> </property> </bean> 

Example usage with MappingJackson2JsonView:

 <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"> <property name="objectMapper"> <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean" p:failOnEmptyBeans="false" p:indentOutput="true"> <property name="serializers"> <array> <bean class="org.mycompany.MyCustomSerializer" /> </array> </property> </bean> </property> </bean> 

In case there are no specific setters provided (for some rarely used options), you can still use the more general methods #setFeaturesToEnable and #setFeaturesToDisable.

 <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"> <property name="featuresToEnable"> <array> <util:constant static-field="com.fasterxml.jackson.databind.SerializationFeature.WRAP_ROOT_VALUE"/> <util:constant static-field="com.fasterxml.jackson.databind.SerializationFeature.CLOSE_CLOSEABLE"/> </array> </property> <property name="featuresToDisable"> <array> <util:constant static-field="com.fasterxml.jackson.databind.MapperFeature.USE_ANNOTATIONS"/> </array> </property> </bean> 

It also automatically registers the following well-known modules if they are detected on the classpath:

In case you want to configure Jackson's ObjectMapper with a custom Module, you can register one or more such Modules by class name via #setModulesToInstall:

 <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"> <property name="modulesToInstall" value="myapp.jackson.MySampleModule,myapp.jackson.MyOtherModule"/> </bean 

Compatible with Jackson 2.6 and higher, as of Spring 4.3.

Author
Dmitry Katsubo

Author
Rossen Stoyanchev

Author
Brian Clozel

Author
Juergen Hoeller

Author
Tadaya Tsuyukubo

Author
Sebastien Deleuze

Since
3.2