Package org.springframework.http.converter.json

# Class Jackson2ObjectMapperFactoryBean

java.lang.Object
org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean

All Implemented Interfaces:
  `org.springframework.beans.factory.Aware`, `org.springframework.beans.factory.BeanClassLoaderAware`, `org.springframework.beans.factory.FactoryBean<ObjectMapper>`, `org.springframework.beans.factory.InitializingBean`, `org.springframework.context.ApplicationContextAware`

public class Jackson2ObjectMapperFactoryBean extends Object implements org.springframework.beans.factory.FactoryBean<ObjectMapper>, org.springframework.beans.factory.BeanClassLoaderAware, org.springframework.context.ApplicationContextAware, org.springframework.beans.factory.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 disabled
- `DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES` is disabled

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:

- jackson-datatype-jdk7: support for Java 7 types like `Path`
- jackson-datatype-jdk8: support for other Java 8 types like `Optional`
- jackson-datatype-jsr310: support for Java 8 Date & Time API types
- jackson-module-kotlin: support for Kotlin classes and data classes

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>

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

## Field Summary

### Fields inherited from interface org.springframework.beans.factory.FactoryBean

`OBJECT_TYPE_ATTRIBUTE`

## Constructor Summary

Constructors
Constructor
Description
`Jackson2ObjectMapperFactoryBean()`

## Method Summary

Modifier and Type
Method
Description
`void`
`afterPropertiesSet()`
`ObjectMapper`
`getObject()`
Return the singleton ObjectMapper.
`Class<?>`
`getObjectType()`
`boolean`
`isSingleton()`
`void`
`setAnnotationIntrospector(AnnotationIntrospector annotationIntrospector)`
Set an `AnnotationIntrospector` for both serialization and deserialization.
`void`
`setApplicationContext(org.springframework.context.ApplicationContext applicationContext)`
Set the builder `ApplicationContext` in order to autowire Jackson handlers (`JsonSerializer`, `JsonDeserializer`, `KeyDeserializer`, `TypeResolverBuilder` and `TypeIdResolver`).
`void`
`setAutoDetectFields(boolean autoDetectFields)`
Shortcut for `MapperFeature.AUTO_DETECT_FIELDS` option.
`void`
`setAutoDetectGettersSetters(boolean autoDetectGettersSetters)`
Shortcut for `MapperFeature.AUTO_DETECT_SETTERS`/ `MapperFeature.AUTO_DETECT_GETTERS`/`MapperFeature.AUTO_DETECT_IS_GETTERS` options.
`void`
`setBeanClassLoader(ClassLoader beanClassLoader)`
`void`
`setCreateXmlMapper(boolean createXmlMapper)`
If set to true and no custom `ObjectMapper` has been set, a `XmlMapper` will be created using its default constructor.
`void`
`setDateFormat(DateFormat dateFormat)`
Define the format for date/time with the given `DateFormat`.
`void`
`setDefaultTyping(TypeResolverBuilder<?> typeResolverBuilder)`
Specify a `TypeResolverBuilder` to use for Jackson's default typing.
`void`
`setDefaultUseWrapper(boolean defaultUseWrapper)`
Define if a wrapper will be used for indexed (List, array) properties or not by default (only applies to `XmlMapper`).
`void`
`setDefaultViewInclusion(boolean defaultViewInclusion)`
Shortcut for `MapperFeature.DEFAULT_VIEW_INCLUSION` option.
`void`
`setDeserializers(JsonDeserializer<?>... deserializers)`
Configure custom deserializers.
`void`
`setDeserializersByType(Map<Class<?>,JsonDeserializer<?>> deserializers)`
Configure custom deserializers for the given types.
`void`
`setFactory(JsonFactory factory)`
Define the `JsonFactory` to be used to create the `ObjectMapper` instance.
`void`
`setFailOnEmptyBeans(boolean failOnEmptyBeans)`
Shortcut for `SerializationFeature.FAIL_ON_EMPTY_BEANS` option.
`void`
`setFailOnUnknownProperties(boolean failOnUnknownProperties)`
Shortcut for `DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES` option.
`void`
`setFeaturesToDisable(Object... featuresToDisable)`
Specify features to disable.
`void`
`setFeaturesToEnable(Object... featuresToEnable)`
Specify features to enable.
`void`
`setFilters(FilterProvider filters)`
Set the global filters to use in order to support `@JsonFilter` annotated POJO.
`void`
`setFindModulesViaServiceLoader(boolean findModules)`
Set whether to let Jackson find available modules via the JDK ServiceLoader, based on META-INF metadata in the classpath.
`void`
`setHandlerInstantiator(HandlerInstantiator handlerInstantiator)`
Customize the construction of Jackson handlers (`JsonSerializer`, `JsonDeserializer`, `KeyDeserializer`, `TypeResolverBuilder` and `TypeIdResolver`).
`void`
`setIndentOutput(boolean indentOutput)`
Shortcut for `SerializationFeature.INDENT_OUTPUT` option.
`void`
`setLocale(Locale locale)`
Override the default `Locale` to use for formatting.
`void`
`setMixIns(Map<Class<?>,Class<?>> mixIns)`
Add mix-in annotations to use for augmenting specified class or interface.
`void`
`setModules(List<Module> modules)`
Set a complete list of modules to be registered with the `ObjectMapper`.
`final void`
`setModulesToInstall(Class<? extends Module>... modules)`
Specify one or more modules by class (or class name in XML) to be registered with the `ObjectMapper`.
`void`
`setObjectMapper(ObjectMapper objectMapper)`
Set the `ObjectMapper` instance to use.
`void`
`setPropertyNamingStrategy(PropertyNamingStrategy propertyNamingStrategy)`
Specify a `PropertyNamingStrategy` to configure the `ObjectMapper` with.
`void`
`setSerializationInclusion(com.fasterxml.jackson.annotation.JsonInclude.Include serializationInclusion)`
Set a custom inclusion strategy for serialization.
`void`
`setSerializers(JsonSerializer<?>... serializers)`
Configure custom serializers.
`void`
`setSerializersByType(Map<Class<?>,JsonSerializer<?>> serializers)`
Configure custom serializers for the given types.
`void`
`setSimpleDateFormat(String format)`
Define the date/time format with a `SimpleDateFormat`.
`void`
`setTimeZone(TimeZone timeZone)`
Override the default `TimeZone` to use for formatting.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### Jackson2ObjectMapperFactoryBean

public Jackson2ObjectMapperFactoryBean()

## Method Details

### setObjectMapper

public void setObjectMapper(ObjectMapper objectMapper)
Set the `ObjectMapper` instance to use. If not set, the `ObjectMapper` will be created using its default constructor.

### setCreateXmlMapper

public void setCreateXmlMapper(boolean createXmlMapper)
If set to true and no custom `ObjectMapper` has been set, a `XmlMapper` will be created using its default constructor.

Since:
  4.1

### setFactory

public void setFactory(JsonFactory factory)
Define the `JsonFactory` to be used to create the `ObjectMapper` instance.

Since:
  5.0

### setDateFormat

public void setDateFormat(DateFormat dateFormat)
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.

See Also:
  - `setSimpleDateFormat(String)`

### setSimpleDateFormat

public void setSimpleDateFormat(String format)
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.

See Also:
  - `setDateFormat(DateFormat)`

### setLocale

public void setLocale(Locale locale)
Override the default `Locale` to use for formatting. Default value used is `Locale.getDefault()`.

Since:
  4.1.5

### setTimeZone

public void setTimeZone(TimeZone timeZone)
Override the default `TimeZone` to use for formatting. Default value used is UTC (NOT local timezone).

Since:
  4.1.5

### setAnnotationIntrospector

public void setAnnotationIntrospector(AnnotationIntrospector annotationIntrospector)
Set an `AnnotationIntrospector` for both serialization and deserialization.

### setPropertyNamingStrategy

public void setPropertyNamingStrategy(PropertyNamingStrategy propertyNamingStrategy)
Specify a `PropertyNamingStrategy` to configure the `ObjectMapper` with.

Since:
  4.0.2

### setDefaultTyping

public void setDefaultTyping(TypeResolverBuilder<?> typeResolverBuilder)
Specify a `TypeResolverBuilder` to use for Jackson's default typing.

Since:
  4.2.2

### setSerializationInclusion

public void setSerializationInclusion(com.fasterxml.jackson.annotation.JsonInclude.Include serializationInclusion)
Set a custom inclusion strategy for serialization.

See Also:
  - `JsonInclude.Include`

### setFilters

public void setFilters(FilterProvider filters)
Set the global filters to use in order to support `@JsonFilter` annotated POJO.

Since:
  4.2
See Also:
  - `Jackson2ObjectMapperBuilder.filters(FilterProvider)`

### setMixIns

public void setMixIns(Map<Class<?>,Class<?>> mixIns)
Add mix-in annotations to use for augmenting specified class or interface.

Parameters:
  `mixIns` - a Map of entries with target classes (or interface) whose annotations to effectively override as key and mix-in classes (or interface) whose annotations are to be "added" to target's annotations as value.
Since:
  4.1.2
See Also:
  - `ObjectMapper.addMixInAnnotations(Class, Class)`

### setSerializers

public void setSerializers(JsonSerializer<?>... serializers)
Configure custom serializers. Each serializer is registered for the type returned by `JsonSerializer.handledType()`, which must not be `null`.

See Also:
  - `setSerializersByType(Map)`

### setSerializersByType

public void setSerializersByType(Map<Class<?>,JsonSerializer<?>> serializers)
Configure custom serializers for the given types.

See Also:
  - `setSerializers(JsonSerializer...)`

### setDeserializers

public void setDeserializers(JsonDeserializer<?>... deserializers)
Configure custom deserializers. Each deserializer is registered for the type returned by `JsonDeserializer.handledType()`, which must not be `null`.

Since:
  4.3
See Also:
  - `setDeserializersByType(Map)`

### setDeserializersByType

public void setDeserializersByType(Map<Class<?>,JsonDeserializer<?>> deserializers)
Configure custom deserializers for the given types.

### setAutoDetectFields

public void setAutoDetectFields(boolean autoDetectFields)
Shortcut for `MapperFeature.AUTO_DETECT_FIELDS` option.

### setAutoDetectGettersSetters

public void setAutoDetectGettersSetters(boolean autoDetectGettersSetters)
Shortcut for `MapperFeature.AUTO_DETECT_SETTERS`/ `MapperFeature.AUTO_DETECT_GETTERS`/`MapperFeature.AUTO_DETECT_IS_GETTERS` options.

### setDefaultViewInclusion

public void setDefaultViewInclusion(boolean defaultViewInclusion)
Shortcut for `MapperFeature.DEFAULT_VIEW_INCLUSION` option.

Since:
  4.1

### setFailOnUnknownProperties

public void setFailOnUnknownProperties(boolean failOnUnknownProperties)
Shortcut for `DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES` option.

Since:
  4.1.1

### setFailOnEmptyBeans

public void setFailOnEmptyBeans(boolean failOnEmptyBeans)
Shortcut for `SerializationFeature.FAIL_ON_EMPTY_BEANS` option.

### setIndentOutput

public void setIndentOutput(boolean indentOutput)
Shortcut for `SerializationFeature.INDENT_OUTPUT` option.

### setDefaultUseWrapper

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

Since:
  4.3

### setFeaturesToEnable

public void setFeaturesToEnable(Object... featuresToEnable)
Specify features to enable.

See Also:
  - `JsonParser.Feature`
  - `JsonGenerator.Feature`
  - `SerializationFeature`
  - `DeserializationFeature`
  - `MapperFeature`

### setFeaturesToDisable

public void setFeaturesToDisable(Object... featuresToDisable)
Specify features to disable.

See Also:
  - `JsonParser.Feature`
  - `JsonGenerator.Feature`
  - `SerializationFeature`
  - `DeserializationFeature`
  - `MapperFeature`

### setModules

public void setModules(List<Module> modules)
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(boolean)`). As a consequence, specifying an empty list here will suppress any kind of module detection.

Specify either this or `setModulesToInstall(java.lang.Class<? extends com.fasterxml.jackson.databind.Module>...)`, not both.

Since:
  4.0
See Also:
  - `Module`

### setModulesToInstall

@SafeVarargs public final void setModulesToInstall(Class<? extends Module>... modules)
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, or Jackson's finding of modules (see `setFindModulesViaServiceLoader(boolean)`), allowing to eventually override their configuration.

Specify either this or `setModules(java.util.List<com.fasterxml.jackson.databind.Module>)`, not both.

Since:
  4.0.1
See Also:
  - `Module`

### setFindModulesViaServiceLoader

public void setFindModulesViaServiceLoader(boolean findModules)
Set whether to let Jackson find available modules via the JDK ServiceLoader, based on META-INF metadata in the classpath.

If this mode is not set, Spring's Jackson2ObjectMapperFactoryBean itself will try to find the JSR-310 support module on the classpath.

Since:
  4.0.1
See Also:
  - `ObjectMapper.findModules()`

### setBeanClassLoader

public void setBeanClassLoader(ClassLoader beanClassLoader)

Specified by:
  `setBeanClassLoader` in interface `org.springframework.beans.factory.BeanClassLoaderAware`

### setHandlerInstantiator

public void setHandlerInstantiator(HandlerInstantiator handlerInstantiator)
Customize the construction of Jackson handlers (`JsonSerializer`, `JsonDeserializer`, `KeyDeserializer`, `TypeResolverBuilder` and `TypeIdResolver`).

Since:
  4.1.3
See Also:
  - `setApplicationContext(ApplicationContext)`

### setApplicationContext

public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext)
Set the builder `ApplicationContext` in order to autowire Jackson handlers (`JsonSerializer`, `JsonDeserializer`, `KeyDeserializer`, `TypeResolverBuilder` and `TypeIdResolver`).

Specified by:
  `setApplicationContext` in interface `org.springframework.context.ApplicationContextAware`
Since:
  4.1.3
See Also:
  - `Jackson2ObjectMapperBuilder.applicationContext(ApplicationContext)`
  - `SpringHandlerInstantiator`

### afterPropertiesSet

public void afterPropertiesSet()

Specified by:
  `afterPropertiesSet` in interface `org.springframework.beans.factory.InitializingBean`

### getObject

@Nullable public ObjectMapper getObject()
Return the singleton ObjectMapper.

Specified by:
  `getObject` in interface `org.springframework.beans.factory.FactoryBean<ObjectMapper>`

### getObjectType

@Nullable public Class<?> getObjectType()

Specified by:
  `getObjectType` in interface `org.springframework.beans.factory.FactoryBean<ObjectMapper>`

### isSingleton

public boolean isSingleton()

Specified by:
  `isSingleton` in interface `org.springframework.beans.factory.FactoryBean<ObjectMapper>`