Class Jackson2ObjectMapperFactoryBean

java.lang.Object
org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean
All Implemented Interfaces:
Aware, BeanClassLoaderAware, FactoryBean<ObjectMapper>, InitializingBean, ApplicationContextAware

public class Jackson2ObjectMapperFactoryBean extends Object implements FactoryBean<ObjectMapper>, BeanClassLoaderAware, ApplicationContextAware, InitializingBean
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(java.lang.Object...) and setFeaturesToDisable(java.lang.Object...).

 <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(java.lang.Class<? extends com.fasterxml.jackson.databind.Module>...):

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

Compatible with Jackson 2.9 to 2.12, as of Spring 5.3.

Since:
3.2
Author:
Dmitry Katsubo, Rossen Stoyanchev, Brian Clozel, Juergen Hoeller, Tadaya Tsuyukubo, Sebastien Deleuze