我遇到了一个无法在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
致以最诚挚的问候和感谢
答案 0 :(得分:0)
尝试
<!--
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>
答案 1 :(得分:0)
解决方案已在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>