【问题标题】:Spring 3.2, Jackson2 ObjectMapper configurationSpring 3.2,Jackson2 ObjectMapper 配置
【发布时间】:2013-01-14 16:01:09
【问题描述】:

我在 ObjectMapper 的配置上有一个无法解决的问题。我需要将其配置为忽略那些没有我的 POJO 的参数...很简单,但我配置了上千种不同的方式,但我无法让它工作。

我的 servlet.xml

<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" >
     <property name="objectMapper">
     <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
         <property name="featuresToDisable">
           <array>
            <util:constant 
            static-field="com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES"/>
          </array>
         </property>
      </bean>                  
    </property>  
</bean> 

我也尝试过扩展 ObjectMapper 类,但得到了相同的结果。我看到映射器配置正确,但我希望 MappingJackson2HttpMessageConverter 接收到不同的 ObjectMapper 实例。我不知道还能做什么让我忽略全局参数。

当我使用应忽略的参数(POJO 上不存在)发出请求时,请求中会出现语法错误。

我正在使用: 春天 3.2.0 杰克逊2.1.2

最好的问候和感谢

【问题讨论】:

    标签: json spring serialization spring-mvc jackson


    【解决方案1】:

    试试

    <!-- 
         Implement a custom ObjectMapper and initialize the features you needed 
         eg. FAIL_ON_UNKNOWN_PROPERTIES 
    -->
    <bean id="jacksonObjectMapper" class="com.sample.CustomObjectMapper"/>
    
    <!-- Replace Spring's default message converter -->
    <mvc:annotation-driven>
      <mvc:message-converters>
        <ref bean="jacksonObjectMapper"/>
      </mvc:message-converters>
    </mvc:annotation-driven>
    

    【讨论】:

      【解决方案2】:

      在 Spring 3.2.18.RELEASE 和 Jackson 2.7.5 上测试的解决方案。

      <?xml version="1.0" encoding="UTF-8"?>
      
      <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
          http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
      
      <bean id="jacksonObjectMapper"
          class="com.fasterxml.jackson.databind.ObjectMapper" />
      
      <bean
          class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
          <property name="targetObject" ref="jacksonObjectMapper" />
          <property name="targetMethod" value="configure" />
          <property name="arguments">
              <list>
                  <value
                      type="com.fasterxml.jackson.databind.DeserializationFeature">FAIL_ON_UNKNOWN_PROPERTIES</value>
                  <value>false</value>
              </list>
          </property>
      </bean>
      
      <mvc:annotation-driven>
          <mvc:message-converters>
              <bean
                  class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                  <property name="objectMapper" ref="jacksonObjectMapper" />
              </bean>
          </mvc:message-converters>
      </mvc:annotation-driven>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-25
        • 1970-01-01
        • 1970-01-01
        • 2023-03-05
        • 2017-05-09
        • 2017-08-28
        • 2019-06-07
        • 2012-10-17
        相关资源
        最近更新 更多