Java Code Examples for org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean

Following code examples demonstrate how to use org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBeanfrom spring-framework. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBeanand various code implementation of this class.

  @Bean
  ObjectMapper objectMapper()
  {
    Jackson2ObjectMapperFactoryBean bean = new org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean();

    bean.setFeaturesToDisable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    bean.afterPropertiesSet();
    return bean.getObject();
  } 


    @Bean
    public Jackson2ObjectMapperFactoryBean jackson2ObjectMapperFactoryBean()
    {
    	return new Jackson2ObjectMapperFactoryBean();
    } 

  
  public static void printNode(Node root)
    {
      Jackson2ObjectMapperFactoryBean bean = new org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean();
      bean.setFeaturesToDisable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
      bean.afterPropertiesSet();
      ObjectMapper objectMapper = bean.getObject();

      try
      {
        String json = objectMapper.writeValueAsString(root);

        System.out.println(json);
      }
      catch (Exception e)
      {
      }
    } 

    public Jackson2ObjectMapperFactoryBean jackson2ObjectMapperFactoryBean() {
        Jackson2ObjectMapperFactoryBean objectMapperFactoryBean = new Jackson2ObjectMapperFactoryBean();
        objectMapperFactoryBean.setFeaturesToDisable(WRITE_DATES_AS_TIMESTAMPS);
        return objectMapperFactoryBean;
    } 

    public Jackson2ObjectMapperFactoryBean jackson2ObjectMapperFactoryBean() {
        Jackson2ObjectMapperFactoryBean factoryBean = new Jackson2ObjectMapperFactoryBean();
        return factoryBean;
    } 

Advertisement
Javadoc
A FactoryBean for creating a Jackson 2.x ObjectMapper (default) or XmlMapper (createXmlMapper proper

ty 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 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: jackson-datatype-jdk7: support for Java 7 types like java.nio.file.Path jackson-datatype-jdk8: support for other Java 8 types like java.util.Optional jackson-datatype-jsr310: support for Java 8 Date & Time API types jackson-datatype-joda: support for Joda-Time 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: <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

Read More
Advertisement