我在ObjectMapper的配置上有一个无法解决的问题。我需要配置它来忽略那些让我的POJO...so不容易的参数,但是我配置了上千种不同的方法,我不能让它工作。
我的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中不存在)发出请求时,会在请求中出现语法错误。
我正在使用: Spring 3.2.0 jackson 2.1.2
致以最良好的问候和感谢
发布于 2013-07-24 07:22:15
试一试
<!--
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>发布于 2020-10-02 19:15:11
<?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>https://stackoverflow.com/questions/14610757
复制相似问题