Class Jackson2ObjectMapperFactoryBean
- All Implemented Interfaces:
Aware, BeanClassLoaderAware, FactoryBean<com.fasterxml.jackson.databind.ObjectMapper>, InitializingBean, ApplicationContextAware
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_INCLUSIONis disabledDeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIESis 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(Object...) and
setFeaturesToDisable(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(Class...):
<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 FactoryBean
OBJECT_TYPE_ATTRIBUTE -
Constructor Summary
ConstructorsConstructorDescriptionDeprecated, for removal: This API element is subject to removal in a future version. -
Method Summary
Modifier and TypeMethodDescriptionvoidDeprecated, for removal: This API element is subject to removal in a future version.Invoked by the containingBeanFactoryafter it has set all bean properties and satisfiedBeanFactoryAware,ApplicationContextAwareetc.@Nullable com.fasterxml.jackson.databind.ObjectMapperDeprecated, for removal: This API element is subject to removal in a future version.Return the singleton ObjectMapper.Deprecated, for removal: This API element is subject to removal in a future version.Return the type of object that this FactoryBean creates, ornullif not known in advance.booleanDeprecated, for removal: This API element is subject to removal in a future version.Is the object managed by this factory a singleton? That is, willFactoryBean.getObject()always return the same object (a reference that can be cached)?voidsetAnnotationIntrospector(com.fasterxml.jackson.databind.AnnotationIntrospector annotationIntrospector) Deprecated, for removal: This API element is subject to removal in a future version.Set anAnnotationIntrospectorfor both serialization and deserialization.voidsetApplicationContext(ApplicationContext applicationContext) Deprecated, for removal: This API element is subject to removal in a future version.Set the builderApplicationContextin order to autowire Jackson handlers (JsonSerializer,JsonDeserializer,KeyDeserializer,TypeResolverBuilderandTypeIdResolver).voidsetAutoDetectFields(boolean autoDetectFields) Deprecated, for removal: This API element is subject to removal in a future version.Shortcut forMapperFeature.AUTO_DETECT_FIELDSoption.voidsetAutoDetectGettersSetters(boolean autoDetectGettersSetters) Deprecated, for removal: This API element is subject to removal in a future version.Shortcut forMapperFeature.AUTO_DETECT_SETTERS/MapperFeature.AUTO_DETECT_GETTERS/MapperFeature.AUTO_DETECT_IS_GETTERSoptions.voidsetBeanClassLoader(ClassLoader beanClassLoader) Deprecated, for removal: This API element is subject to removal in a future version.Callback that supplies the beanclass loaderto a bean instance.voidsetCreateXmlMapper(boolean createXmlMapper) Deprecated, for removal: This API element is subject to removal in a future version.If set to true and no customObjectMapperhas been set, aXmlMapperwill be created using its default constructor.voidsetDateFormat(DateFormat dateFormat) Deprecated, for removal: This API element is subject to removal in a future version.Define the format for date/time with the givenDateFormat.voidsetDefaultTyping(com.fasterxml.jackson.databind.jsontype.TypeResolverBuilder<?> typeResolverBuilder) Deprecated, for removal: This API element is subject to removal in a future version.Specify aTypeResolverBuilderto use for Jackson's default typing.voidsetDefaultUseWrapper(boolean defaultUseWrapper) Deprecated, for removal: This API element is subject to removal in a future version.Define if a wrapper will be used for indexed (List, array) properties or not by default (only applies toXmlMapper).voidsetDefaultViewInclusion(boolean defaultViewInclusion) Deprecated, for removal: This API element is subject to removal in a future version.Shortcut forMapperFeature.DEFAULT_VIEW_INCLUSIONoption.voidsetDeserializers(com.fasterxml.jackson.databind.JsonDeserializer<?>... deserializers) Deprecated, for removal: This API element is subject to removal in a future version.Configure custom deserializers.voidsetDeserializersByType(Map<Class<?>, com.fasterxml.jackson.databind.JsonDeserializer<?>> deserializers) Deprecated, for removal: This API element is subject to removal in a future version.Configure custom deserializers for the given types.voidsetFactory(com.fasterxml.jackson.core.JsonFactory factory) Deprecated, for removal: This API element is subject to removal in a future version.Define theJsonFactoryto be used to create theObjectMapperinstance.voidsetFailOnEmptyBeans(boolean failOnEmptyBeans) Deprecated, for removal: This API element is subject to removal in a future version.Shortcut forSerializationFeature.FAIL_ON_EMPTY_BEANSoption.voidsetFailOnUnknownProperties(boolean failOnUnknownProperties) Deprecated, for removal: This API element is subject to removal in a future version.Shortcut forDeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIESoption.voidsetFeaturesToDisable(Object... featuresToDisable) Deprecated, for removal: This API element is subject to removal in a future version.Specify features to disable.voidsetFeaturesToEnable(Object... featuresToEnable) Deprecated, for removal: This API element is subject to removal in a future version.Specify features to enable.voidsetFilters(com.fasterxml.jackson.databind.ser.FilterProvider filters) Deprecated, for removal: This API element is subject to removal in a future version.Set the global filters to use in order to support@JsonFilterannotated POJO.voidsetFindModulesViaServiceLoader(boolean findModules) Deprecated, for removal: This API element is subject to removal in a future version.Set whether to let Jackson find available modules via the JDK ServiceLoader, based on META-INF metadata in the classpath.voidsetHandlerInstantiator(com.fasterxml.jackson.databind.cfg.HandlerInstantiator handlerInstantiator) Deprecated, for removal: This API element is subject to removal in a future version.Customize the construction of Jackson handlers (JsonSerializer,JsonDeserializer,KeyDeserializer,TypeResolverBuilderandTypeIdResolver).voidsetIndentOutput(boolean indentOutput) Deprecated, for removal: This API element is subject to removal in a future version.Shortcut forSerializationFeature.INDENT_OUTPUToption.voidDeprecated, for removal: This API element is subject to removal in a future version.Override the defaultLocaleto use for formatting.voidDeprecated, for removal: This API element is subject to removal in a future version.Add mix-in annotations to use for augmenting specified class or interface.voidsetModules(List<com.fasterxml.jackson.databind.Module> modules) Deprecated, for removal: This API element is subject to removal in a future version.Set a complete list of modules to be registered with theObjectMapper.final voidsetModulesToInstall(Class<? extends com.fasterxml.jackson.databind.Module>... modules) Deprecated, for removal: This API element is subject to removal in a future version.Specify one or more modules by class (or class name in XML) to be registered with theObjectMapper.voidsetObjectMapper(com.fasterxml.jackson.databind.ObjectMapper objectMapper) Deprecated, for removal: This API element is subject to removal in a future version.Set theObjectMapperinstance to use.voidsetPropertyNamingStrategy(com.fasterxml.jackson.databind.PropertyNamingStrategy propertyNamingStrategy) Deprecated, for removal: This API element is subject to removal in a future version.Specify aPropertyNamingStrategyto configure theObjectMapperwith.voidsetSerializationInclusion(com.fasterxml.jackson.annotation.JsonInclude.Include serializationInclusion) Deprecated, for removal: This API element is subject to removal in a future version.Set a custom inclusion strategy for serialization.voidsetSerializers(com.fasterxml.jackson.databind.JsonSerializer<?>... serializers) Deprecated, for removal: This API element is subject to removal in a future version.Configure custom serializers.voidsetSerializersByType(Map<Class<?>, com.fasterxml.jackson.databind.JsonSerializer<?>> serializers) Deprecated, for removal: This API element is subject to removal in a future version.Configure custom serializers for the given types.voidsetSimpleDateFormat(String format) Deprecated, for removal: This API element is subject to removal in a future version.Define the date/time format with aSimpleDateFormat.voidsetTimeZone(TimeZone timeZone) Deprecated, for removal: This API element is subject to removal in a future version.Override the defaultTimeZoneto use for formatting.
-
Constructor Details
-
Jackson2ObjectMapperFactoryBean
public Jackson2ObjectMapperFactoryBean()Deprecated, for removal: This API element is subject to removal in a future version.
-
-
Method Details
-
setObjectMapper
public void setObjectMapper(com.fasterxml.jackson.databind.ObjectMapper objectMapper) Deprecated, for removal: This API element is subject to removal in a future version.Set theObjectMapperinstance to use. If not set, theObjectMapperwill be created using its default constructor. -
setCreateXmlMapper
public void setCreateXmlMapper(boolean createXmlMapper) Deprecated, for removal: This API element is subject to removal in a future version.If set to true and no customObjectMapperhas been set, aXmlMapperwill be created using its default constructor.- Since:
- 4.1
-
setFactory
public void setFactory(com.fasterxml.jackson.core.JsonFactory factory) Deprecated, for removal: This API element is subject to removal in a future version.Define theJsonFactoryto be used to create theObjectMapperinstance.- Since:
- 5.0
-
setDateFormat
Deprecated, for removal: This API element is subject to removal in a future version.Define the format for date/time with the givenDateFormat.Note: Setting this property makes the exposed
ObjectMappernon-thread-safe, according to Jackson's thread safety rules.- See Also:
-
setSimpleDateFormat
Deprecated, for removal: This API element is subject to removal in a future version.Define the date/time format with aSimpleDateFormat.Note: Setting this property makes the exposed
ObjectMappernon-thread-safe, according to Jackson's thread safety rules.- See Also:
-
setLocale
Deprecated, for removal: This API element is subject to removal in a future version.Override the defaultLocaleto use for formatting. Default value used isLocale.getDefault().- Since:
- 4.1.5
-
setTimeZone
-
setAnnotationIntrospector
public void setAnnotationIntrospector(com.fasterxml.jackson.databind.AnnotationIntrospector annotationIntrospector) Deprecated, for removal: This API element is subject to removal in a future version.Set anAnnotationIntrospectorfor both serialization and deserialization. -
setPropertyNamingStrategy
public void setPropertyNamingStrategy(com.fasterxml.jackson.databind.PropertyNamingStrategy propertyNamingStrategy) Deprecated, for removal: This API element is subject to removal in a future version.Specify aPropertyNamingStrategyto configure theObjectMapperwith.- Since:
- 4.0.2
-
setDefaultTyping
public void setDefaultTyping(com.fasterxml.jackson.databind.jsontype.TypeResolverBuilder<?> typeResolverBuilder) Deprecated, for removal: This API element is subject to removal in a future version.Specify aTypeResolverBuilderto use for Jackson's default typing.- Since:
- 4.2.2
-
setSerializationInclusion
public void setSerializationInclusion(com.fasterxml.jackson.annotation.JsonInclude.Include serializationInclusion) Deprecated, for removal: This API element is subject to removal in a future version.Set a custom inclusion strategy for serialization.- See Also:
-
setFilters
public void setFilters(com.fasterxml.jackson.databind.ser.FilterProvider filters) Deprecated, for removal: This API element is subject to removal in a future version.Set the global filters to use in order to support@JsonFilterannotated POJO.- Since:
- 4.2
- See Also:
-
setMixIns
Deprecated, for removal: This API element is subject to removal in a future version.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:
-
setSerializers
public void setSerializers(com.fasterxml.jackson.databind.JsonSerializer<?>... serializers) Deprecated, for removal: This API element is subject to removal in a future version.Configure custom serializers. Each serializer is registered for the type returned byJsonSerializer.handledType(), which must not benull.- See Also:
-
setSerializersByType
-
setDeserializers
public void setDeserializers(com.fasterxml.jackson.databind.JsonDeserializer<?>... deserializers) Deprecated, for removal: This API element is subject to removal in a future version.Configure custom deserializers. Each deserializer is registered for the type returned byJsonDeserializer.handledType(), which must not benull.- Since:
- 4.3
- See Also:
-
setDeserializersByType
-
setAutoDetectFields
public void setAutoDetectFields(boolean autoDetectFields) Deprecated, for removal: This API element is subject to removal in a future version.Shortcut forMapperFeature.AUTO_DETECT_FIELDSoption. -
setAutoDetectGettersSetters
public void setAutoDetectGettersSetters(boolean autoDetectGettersSetters) Deprecated, for removal: This API element is subject to removal in a future version.Shortcut forMapperFeature.AUTO_DETECT_SETTERS/MapperFeature.AUTO_DETECT_GETTERS/MapperFeature.AUTO_DETECT_IS_GETTERSoptions. -
setDefaultViewInclusion
public void setDefaultViewInclusion(boolean defaultViewInclusion) Deprecated, for removal: This API element is subject to removal in a future version.Shortcut forMapperFeature.DEFAULT_VIEW_INCLUSIONoption.- Since:
- 4.1
-
setFailOnUnknownProperties
public void setFailOnUnknownProperties(boolean failOnUnknownProperties) Deprecated, for removal: This API element is subject to removal in a future version.Shortcut forDeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIESoption.- Since:
- 4.1.1
-
setFailOnEmptyBeans
public void setFailOnEmptyBeans(boolean failOnEmptyBeans) Deprecated, for removal: This API element is subject to removal in a future version.Shortcut forSerializationFeature.FAIL_ON_EMPTY_BEANSoption. -
setIndentOutput
public void setIndentOutput(boolean indentOutput) Deprecated, for removal: This API element is subject to removal in a future version.Shortcut forSerializationFeature.INDENT_OUTPUToption. -
setDefaultUseWrapper
public void setDefaultUseWrapper(boolean defaultUseWrapper) Deprecated, for removal: This API element is subject to removal in a future version.Define if a wrapper will be used for indexed (List, array) properties or not by default (only applies toXmlMapper).- Since:
- 4.3
-
setFeaturesToEnable
Deprecated, for removal: This API element is subject to removal in a future version.Specify features to enable.- See Also:
-
setFeaturesToDisable
Deprecated, for removal: This API element is subject to removal in a future version.Specify features to disable.- See Also:
-
setModules
Deprecated, for removal: This API element is subject to removal in a future version.Set a complete list of modules to be registered with theObjectMapper.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(Class...), not both.- Since:
- 4.0
- See Also:
-
setModulesToInstall
@SafeVarargs public final void setModulesToInstall(Class<? extends com.fasterxml.jackson.databind.Module>... modules) Deprecated, for removal: This API element is subject to removal in a future version.Specify one or more modules by class (or class name in XML) to be registered with theObjectMapper.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(List), not both.- Since:
- 4.0.1
- See Also:
-
setFindModulesViaServiceLoader
public void setFindModulesViaServiceLoader(boolean findModules) Deprecated, for removal: This API element is subject to removal in a future version.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:
-
setBeanClassLoader
Deprecated, for removal: This API element is subject to removal in a future version.Description copied from interface:BeanClassLoaderAwareCallback that supplies the beanclass loaderto a bean instance.Invoked after the population of normal bean properties but before an initialization callback such as
InitializingBean'sInitializingBean.afterPropertiesSet()method or a custom init-method.- Specified by:
setBeanClassLoaderin interfaceBeanClassLoaderAware- Parameters:
beanClassLoader- the owning class loader
-
setHandlerInstantiator
public void setHandlerInstantiator(com.fasterxml.jackson.databind.cfg.HandlerInstantiator handlerInstantiator) Deprecated, for removal: This API element is subject to removal in a future version.Customize the construction of Jackson handlers (JsonSerializer,JsonDeserializer,KeyDeserializer,TypeResolverBuilderandTypeIdResolver).- Since:
- 4.1.3
- See Also:
-
setApplicationContext
Deprecated, for removal: This API element is subject to removal in a future version.Set the builderApplicationContextin order to autowire Jackson handlers (JsonSerializer,JsonDeserializer,KeyDeserializer,TypeResolverBuilderandTypeIdResolver).- Specified by:
setApplicationContextin interfaceApplicationContextAware- Parameters:
applicationContext- the ApplicationContext object to be used by this object- Since:
- 4.1.3
- See Also:
-
afterPropertiesSet
public void afterPropertiesSet()Deprecated, for removal: This API element is subject to removal in a future version.Description copied from interface:InitializingBeanInvoked by the containingBeanFactoryafter it has set all bean properties and satisfiedBeanFactoryAware,ApplicationContextAwareetc.This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.
- Specified by:
afterPropertiesSetin interfaceInitializingBean
-
getObject
Deprecated, for removal: This API element is subject to removal in a future version.Return the singleton ObjectMapper.- Specified by:
getObjectin interfaceFactoryBean<com.fasterxml.jackson.databind.ObjectMapper>- Returns:
- an instance of the bean (can be
null) - See Also:
-
getObjectType
Deprecated, for removal: This API element is subject to removal in a future version.Description copied from interface:FactoryBeanReturn the type of object that this FactoryBean creates, ornullif not known in advance.This allows one to check for specific types of beans without instantiating objects, for example on autowiring.
In the case of implementations that create a singleton object, this method should try to avoid singleton creation as far as possible; it should rather estimate the type in advance. For prototypes, returning a meaningful type here is advisable too.
This method can be called before this FactoryBean has been fully initialized. It must not rely on state created during initialization; of course, it can still use such state if available.
NOTE: Autowiring will simply ignore FactoryBeans that return
nullhere. Therefore, it is highly recommended to implement this method properly, using the current state of the FactoryBean.- Specified by:
getObjectTypein interfaceFactoryBean<com.fasterxml.jackson.databind.ObjectMapper>- Returns:
- the type of object that this FactoryBean creates,
or
nullif not known at the time of the call - See Also:
-
isSingleton
public boolean isSingleton()Deprecated, for removal: This API element is subject to removal in a future version.Description copied from interface:FactoryBeanIs the object managed by this factory a singleton? That is, willFactoryBean.getObject()always return the same object (a reference that can be cached)?NOTE: If a FactoryBean indicates that it holds a singleton object, the object returned from
getObject()might get cached by the owning BeanFactory. Hence, do not returntrueunless the FactoryBean always exposes the same reference.The singleton status of the FactoryBean itself will generally be provided by the owning BeanFactory; usually, it has to be defined as singleton there.
NOTE: This method returning
falsedoes not necessarily indicate that returned objects are independent instances. An implementation of the extendedSmartFactoryBeaninterface may explicitly indicate independent instances through itsSmartFactoryBean.isPrototype()method. PlainFactoryBeanimplementations which do not implement this extended interface are simply assumed to always return independent instances if theisSingleton()implementation returnsfalse.The default implementation returns
true, since aFactoryBeantypically manages a singleton instance.- Specified by:
isSingletonin interfaceFactoryBean<com.fasterxml.jackson.databind.ObjectMapper>- Returns:
- whether the exposed object is a singleton
- See Also:
-