62

杰克逊使用以下方法忽略类中的未知属性的注释:

@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在服务器启动时无需编写额外的类?

4

3 回答 3

56

对于 jackson 1.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;
    }
}

对于 jackson 2.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;
    }
}

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

public class JerseyConfig extends ResourceConfig {
    public JerseyConfig() {
        register(JerseyObjectMapperProvider.class);
    }
}
于 2014-04-05T00:04:41.847 回答
40

这可以使用 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>
于 2013-01-15T17:36:19.193 回答
20

对于较新的 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>
于 2013-10-24T00:08:08.680 回答