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:
MapperFeature#DEFAULT_VIEW_INCLUSION
is disabledDeserializationFeature#FAIL_ON_UNKNOWN_PROPERTIES
is disabledExample 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
Jackson2ObjectMapperFactoryBean()
A FactoryBean for creating a Jackson 2.x ObjectMapper (default) or XmlMapper ( It customizes Jackson defaults properties with the following ones:
Example usage with MappingJackson2HttpMessageConverter:
Example usage with MappingJackson2JsonView:
In case there are no specific setters provided (for some rarely used options), you can still use the more general methods
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
Compatible with Jackson 2.6 and higher, as of Spring 4.3. |
open fun afterPropertiesSet(): Unit |
|
open fun getObject(): ObjectMapper
Return the singleton ObjectMapper. |
|
open fun getObjectType(): Class<*> |
|
open fun isSingleton(): Boolean |
|
open fun setAnnotationIntrospector(annotationIntrospector: AnnotationIntrospector): Unit
Set an AnnotationIntrospector for both serialization and deserialization. |
|
open fun setApplicationContext(applicationContext: ApplicationContext): Unit
Set the builder ApplicationContext in order to autowire Jackson handlers (JsonSerializer, JsonDeserializer, KeyDeserializer, |
|
open fun setAutoDetectFields(autoDetectFields: Boolean): Unit
Shortcut for |
|
open fun setAutoDetectGettersSetters(autoDetectGettersSetters: Boolean): Unit
Shortcut for |
|
open fun setBeanClassLoader(beanClassLoader: ClassLoader): Unit |
|
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. |
|
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. |
|
open fun setDefaultTyping(typeResolverBuilder: TypeResolverBuilder<*>): Unit
Specify a TypeResolverBuilder to use for Jackson's default typing. |
|
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). |
|
open fun setDefaultViewInclusion(defaultViewInclusion: Boolean): Unit
Shortcut for |
|
open fun setDeserializers(vararg deserializers: JsonDeserializer<*>): Unit
Configure custom deserializers. Each deserializer is registered for the type returned by |
|
open fun setDeserializersByType(deserializers: MutableMap<Class<*>, JsonDeserializer<*>>): Unit
Configure custom deserializers for the given types. |
|
open fun setFactory(factory: JsonFactory): Unit
Define the JsonFactory to be used to create the ObjectMapper instance. |
|
open fun setFailOnEmptyBeans(failOnEmptyBeans: Boolean): Unit
Shortcut for |
|
open fun setFailOnUnknownProperties(failOnUnknownProperties: Boolean): Unit
Shortcut for |
|
open fun setFeaturesToDisable(vararg featuresToDisable: Any): Unit
Specify features to disable. |
|
open fun setFeaturesToEnable(vararg featuresToEnable: Any): Unit
Specify features to enable. |
|
open fun setFilters(filters: FilterProvider): Unit
Set the global filters to use in order to support JsonFilter annotated POJO. |
|
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. |
|
open fun setHandlerInstantiator(handlerInstantiator: HandlerInstantiator): Unit
Customize the construction of Jackson handlers (JsonSerializer, JsonDeserializer, KeyDeserializer, |
|
open fun setIndentOutput(indentOutput: Boolean): Unit
Shortcut for |
|
open fun setLocale(locale: Locale): Unit
Override the default Locale to use for formatting. Default value used is |
|
open fun setMixIns(mixIns: MutableMap<Class<*>, Class<*>>): Unit
Add mix-in annotations to use for augmenting specified class or interface. |
|
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 Specify either this or |
|
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 Specify either this or |
|
open fun setObjectMapper(objectMapper: ObjectMapper): Unit
Set the ObjectMapper instance to use. If not set, the ObjectMapper will be created using its default constructor. |
|
open fun setPropertyNamingStrategy(propertyNamingStrategy: PropertyNamingStrategy): Unit
Specify a com.fasterxml.jackson.databind.PropertyNamingStrategy to configure the ObjectMapper with. |
|
open fun setSerializationInclusion(serializationInclusion: Include): Unit
Set a custom inclusion strategy for serialization. |
|
open fun setSerializers(vararg serializers: JsonSerializer<*>): Unit
Configure custom serializers. Each serializer is registered for the type returned by |
|
open fun setSerializersByType(serializers: MutableMap<Class<*>, JsonSerializer<*>>): Unit
Configure custom serializers for the given types. |
|
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. |
|
open fun setTimeZone(timeZone: TimeZone): Unit
Override the default TimeZone to use for formatting. Default value used is UTC (NOT local timezone). |