spring-framework / org.springframework.http.converter.json / Jackson2ObjectMapperFactoryBean

Jackson2ObjectMapperFactoryBean

open class Jackson2ObjectMapperFactoryBean : 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 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

Constructors

<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.

Functions

afterPropertiesSet

open fun afterPropertiesSet(): Unit

getObject

open fun getObject(): ObjectMapper

Return the singleton ObjectMapper.

getObjectType

open fun getObjectType(): Class<*>

isSingleton

open fun isSingleton(): Boolean

setAnnotationIntrospector

open fun setAnnotationIntrospector(annotationIntrospector: AnnotationIntrospector): Unit

Set an AnnotationIntrospector for both serialization and deserialization.

setApplicationContext

open fun setApplicationContext(applicationContext: ApplicationContext): Unit

Set the builder ApplicationContext in order to autowire Jackson handlers (JsonSerializer, JsonDeserializer, KeyDeserializer, TypeResolverBuilder and TypeIdResolver).

setAutoDetectFields

open fun setAutoDetectFields(autoDetectFields: Boolean): Unit

Shortcut for MapperFeature#AUTO_DETECT_FIELDS option.

setAutoDetectGettersSetters

open fun setAutoDetectGettersSetters(autoDetectGettersSetters: Boolean): Unit

Shortcut for MapperFeature#AUTO_DETECT_SETTERS/ MapperFeature#AUTO_DETECT_GETTERS/MapperFeature#AUTO_DETECT_IS_GETTERS options.

setBeanClassLoader

open fun setBeanClassLoader(beanClassLoader: ClassLoader): Unit

setCreateXmlMapper

open fun setCreateXmlMapper(createXmlMapper: Boolean): Unit

If set to true and no custom ObjectMapper has been set, a XmlMapper will be created using its default constructor.

setDateFormat

open fun setDateFormat(dateFormat: DateFormat): Unit

Define the format for date/time with the given DateFormat.

Note: Setting this property makes the exposed ObjectMapper non-thread-safe, according to Jackson's thread safety rules.

setDefaultTyping

open fun setDefaultTyping(typeResolverBuilder: TypeResolverBuilder<*>): Unit

Specify a TypeResolverBuilder to use for Jackson's default typing.

setDefaultUseWrapper

open fun setDefaultUseWrapper(defaultUseWrapper: Boolean): Unit

Define if a wrapper will be used for indexed (List, array) properties or not by default (only applies to XmlMapper).

setDefaultViewInclusion

open fun setDefaultViewInclusion(defaultViewInclusion: Boolean): Unit

Shortcut for MapperFeature#DEFAULT_VIEW_INCLUSION option.

setDeserializers

open fun setDeserializers(vararg deserializers: JsonDeserializer<*>): Unit

Configure custom deserializers. Each deserializer is registered for the type returned by JsonDeserializer#handledType(), which must not be null.

setDeserializersByType

open fun setDeserializersByType(deserializers: MutableMap<Class<*>, JsonDeserializer<*>>): Unit

Configure custom deserializers for the given types.

setFactory

open fun setFactory(factory: JsonFactory): Unit

Define the JsonFactory to be used to create the ObjectMapper instance.

setFailOnEmptyBeans

open fun setFailOnEmptyBeans(failOnEmptyBeans: Boolean): Unit

Shortcut for SerializationFeature#FAIL_ON_EMPTY_BEANS option.

setFailOnUnknownProperties

open fun setFailOnUnknownProperties(failOnUnknownProperties: Boolean): Unit

Shortcut for DeserializationFeature#FAIL_ON_UNKNOWN_PROPERTIES option.

setFeaturesToDisable

open fun setFeaturesToDisable(vararg featuresToDisable: Any): Unit

Specify features to disable.

setFeaturesToEnable

open fun setFeaturesToEnable(vararg featuresToEnable: Any): Unit

Specify features to enable.

setFilters

open fun setFilters(filters: FilterProvider): Unit

Set the global filters to use in order to support JsonFilter annotated POJO.

setFindModulesViaServiceLoader

open fun setFindModulesViaServiceLoader(findModules: Boolean): Unit

Set whether to let Jackson find available modules via the JDK ServiceLoader, based on META-INF metadata in the classpath. Requires Jackson 2.2 or higher.

If this mode is not set, Spring's Jackson2ObjectMapperFactoryBean itself will try to find the JSR-310 and Joda-Time support modules on the classpath - provided that Java 8 and Joda-Time themselves are available, respectively.

setHandlerInstantiator

open fun setHandlerInstantiator(handlerInstantiator: HandlerInstantiator): Unit

Customize the construction of Jackson handlers (JsonSerializer, JsonDeserializer, KeyDeserializer, TypeResolverBuilder and TypeIdResolver).

setIndentOutput

open fun setIndentOutput(indentOutput: Boolean): Unit

Shortcut for SerializationFeature#INDENT_OUTPUT option.

setLocale

open fun setLocale(locale: Locale): Unit

Override the default Locale to use for formatting. Default value used is Locale#getDefault().

setMixIns

open fun setMixIns(mixIns: MutableMap<Class<*>, Class<*>>): Unit

Add mix-in annotations to use for augmenting specified class or interface.

setModules

open fun setModules(modules: MutableList<Module>): Unit

Set a complete list of modules to be registered with the ObjectMapper.

Note: If this is set, no finding of modules is going to happen - not by Jackson, and not by Spring either (see #setFindModulesViaServiceLoader). As a consequence, specifying an empty list here will suppress any kind of module detection.

Specify either this or #setModulesToInstall, not both.

setModulesToInstall

open fun setModulesToInstall(vararg modules: Class<out Module>): Unit

Specify one or more modules by class (or class name in XML) to be registered with the ObjectMapper.

Modules specified here will be registered after Spring's autodetection of JSR-310 and Joda-Time, or Jackson's finding of modules (see #setFindModulesViaServiceLoader), allowing to eventually override their configuration.

Specify either this or #setModules, not both.

setObjectMapper

open fun setObjectMapper(objectMapper: ObjectMapper): Unit

Set the ObjectMapper instance to use. If not set, the ObjectMapper will be created using its default constructor.

setPropertyNamingStrategy

open fun setPropertyNamingStrategy(propertyNamingStrategy: PropertyNamingStrategy): Unit

Specify a com.fasterxml.jackson.databind.PropertyNamingStrategy to configure the ObjectMapper with.

setSerializationInclusion

open fun setSerializationInclusion(serializationInclusion: Include): Unit

Set a custom inclusion strategy for serialization.

setSerializers

open fun setSerializers(vararg serializers: JsonSerializer<*>): Unit

Configure custom serializers. Each serializer is registered for the type returned by JsonSerializer#handledType(), which must not be null.

setSerializersByType

open fun setSerializersByType(serializers: MutableMap<Class<*>, JsonSerializer<*>>): Unit

Configure custom serializers for the given types.

setSimpleDateFormat

open fun setSimpleDateFormat(format: String): Unit

Define the date/time format with a SimpleDateFormat.

Note: Setting this property makes the exposed ObjectMapper non-thread-safe, according to Jackson's thread safety rules.

setTimeZone

open fun setTimeZone(timeZone: TimeZone): Unit

Override the default TimeZone to use for formatting. Default value used is UTC (NOT local timezone).