你如何在全球范围内设置jackson忽略spring的不明物业?

jackson有注释忽略类中的未知属性使用:

@JsonIgnoreProperties(ignoreUnknown = true) 

它允许您使用此批注忽略特定的属性:

 @JsonIgnore 

如果你想全局设置它,你可以修改对象映射器:

 // jackson 1.9 and before objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); // or jackson 2.0 objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); 

如何在全局使用spring来设置它,以便在服务器启动时可以使用@Autowired ,而无需编写额外的类?

这可以通过使用Spring的MethodInvokingFactoryBean来实现:

 <!-- Jackson Mapper --> <bean id="jacksonObjectMapper" class="org.codehaus.jackson.map.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="org.codehaus.jackson.map.DeserializationConfig.Feature">FAIL_ON_UNKNOWN_PROPERTIES</value> <value>false</value> </list> </property> </bean> 

这可以像这样连线到一个RestTemplate:

 <bean id="restTemplate" class="org.springframework.web.client.RestTemplate"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="objectMapper" ref="jacksonObjectMapper" /> </bean> </list> </property> </bean> 

它也可以直接注入到消息转换器中,与Spring MVC一起使用:

 <mvc:annotation-driven> <mvc:message-converters> <!-- Jackson converter for HTTP messages --> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="objectMapper" ref="jacksonObjectMapper" /> </bean> </mvc:message-converters> </mvc:annotation-driven> 

对于jackson1.9x或更低版本,您可以忽略对象映射器提供程序的未知属性

 @Provider @Component public class JerseyObjectMapperProvider implements ContextResolver<ObjectMapper> { @Override public ObjectMapper getContext(Class<?> type) { ObjectMapper result = new ObjectMapper(); result.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); return result; } } 

对于jackson2.x及以上版本,您可以使用对象映射器提供程序忽略未知属性

 @Provider @Component public class JerseyObjectMapperProvider implements ContextResolver<ObjectMapper> { @Override public ObjectMapper getContext(Class<?> type) { ObjectMapper result = new ObjectMapper(); result.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); return result; } } 

Jersey类不是由Spring自动发现的。 必须手动注册它们。

 public class JerseyConfig extends ResourceConfig { public JerseyConfig() { register(JerseyObjectMapperProvider.class); } } 

对于较新的jackson版本(2.x),有一些变化:

 <!-- Jackson Mapper --> <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>