首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring 3.2,Jackson2 ObjectMapper配置

Spring 3.2,Jackson2 ObjectMapper配置
EN

Stack Overflow用户
提问于 2013-01-31 02:06:52
回答 2查看 1.8K关注 0票数 1

我在ObjectMapper的配置上有一个无法解决的问题。我需要配置它来忽略那些让我的POJO...so不容易的参数,但是我配置了上千种不同的方法,我不能让它工作。

我的servlet.xml

代码语言:javascript
复制
<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中不存在)发出请求时,会在请求中出现语法错误。

我正在使用: Spring 3.2.0 jackson 2.1.2

致以最良好的问候和感谢

EN

回答 2

Stack Overflow用户

发布于 2013-07-24 07:22:15

试一试

代码语言:javascript
复制
<!-- 
     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>
票数 0
EN

Stack Overflow用户

发布于 2020-10-02 19:15:11

代码语言:javascript
复制
<?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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14610757

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档