Class Jackson2ObjectMapperFactoryBean

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

    public class Jackson2ObjectMapperFactoryBean
    extends java.lang.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:annotationIntrospector-ref="jaxbAnnotationIntrospector" />

    Example usage with MappingJackson2JsonView:

     <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
       <property name="objectMapper">
         <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"
           <property name="serializers">
               <bean class="org.mycompany.MyCustomSerializer" />

    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">
           <util:constant static-field="com.fasterxml.jackson.databind.SerializationFeature.WRAP_ROOT_VALUE"/>
           <util:constant static-field="com.fasterxml.jackson.databind.SerializationFeature.CLOSE_CLOSEABLE"/>
       <property name="featuresToDisable">
           <util:constant static-field="com.fasterxml.jackson.databind.MapperFeature.USE_ANNOTATIONS"/>

    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"/>

    Compatible with Jackson 2.6 and higher, as of Spring 4.3.

    Dmitry Katsubo, Rossen Stoyanchev, Brian Clozel, Juergen Hoeller, Tadaya Tsuyukubo, Sebastien Deleuze
    • Constructor Detail

      • Jackson2ObjectMapperFactoryBean

        public Jackson2ObjectMapperFactoryBean()
    • Method Detail

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

        public void setDateFormat(java.text.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

        public void setSimpleDateFormat(java.lang.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:
      • setLocale

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

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

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

        public void setMixIns(java.util.Map<java.lang.Class<?>,java.lang.Class<?>> mixIns)
        Add mix-in annotations to use for augmenting specified class or interface.
        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.
        See Also:
        ObjectMapper.addMixInAnnotations(Class, Class)
      • setDeserializersByType

        public void setDeserializersByType(java.util.Map<java.lang.Class<?>,JsonDeserializer<?>> deserializers)
        Configure custom deserializers for the given types.
      • 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).
      • 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. 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.

        See Also:
      • afterPropertiesSet

        public void afterPropertiesSet()
        Description copied from interface: InitializingBean
        Invoked by the containing BeanFactory after it has set all bean properties and satisfied BeanFactoryAware, ApplicationContextAware etc.

        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:
        afterPropertiesSet in interface InitializingBean
      • getObjectType

        public java.lang.Class<?> getObjectType()
        Description copied from interface: FactoryBean
        Return the type of object that this FactoryBean creates, or null if 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 are creating 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 null here. Therefore it is highly recommended to implement this method properly, using the current state of the FactoryBean.

        Specified by:
        getObjectType in interface FactoryBean<ObjectMapper>
        the type of object that this FactoryBean creates, or null if not known at the time of the call
        See Also:
      • isSingleton

        public boolean isSingleton()
        Description copied from interface: FactoryBean
        Is the object managed by this factory a singleton? That is, will FactoryBean.getObject() always return the same object (a reference that can be cached)?

        NOTE: If a FactoryBean indicates to hold a singleton object, the object returned from getObject() might get cached by the owning BeanFactory. Hence, do not return true unless 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 false does not necessarily indicate that returned objects are independent instances. An implementation of the extended SmartFactoryBean interface may explicitly indicate independent instances through its SmartFactoryBean.isPrototype() method. Plain FactoryBean implementations which do not implement this extended interface are simply assumed to always return independent instances if the isSingleton() implementation returns false.

        The default implementation returns true, since a FactoryBean typically manages a singleton instance.

        Specified by:
        isSingleton in interface FactoryBean<ObjectMapper>
        whether the exposed object is a singleton
        See Also:
        FactoryBean.getObject(), SmartFactoryBean.isPrototype()