xml 您如何全局设置 Jackson 以忽略 Spring 中的未知属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14343477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:51:11  来源:igfitidea点击:

How do you globally set Hymanson to ignore unknown properties within Spring?

xmlspringHymanson

提问by jnrcorp

Hymanson has annotations for ignoring unknown properties within a class using:

Hymanson 使用以下注释忽略类中的未知属性:

@JsonIgnoreProperties(ignoreUnknown = true) 

It allows you to ignore a specific property using this annotation:

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

@JsonIgnore

If you'd like to globally set it you can modify the object mapper:

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

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

How do you set this globally using spring so it can be @Autowiredat server start up without writing additional classes?

您如何使用 spring 全局设置它,以便它可以@Autowired在服务器启动时无需编写其他类?

采纳答案by jnrcorp

This can be achieved using spring's MethodInvokingFactoryBean:

这可以使用 spring 的 MethodInvokingFactoryBean 来实现:

<!-- Hymanson Mapper -->
<bean id="HymansonObjectMapper" class="org.codehaus.Hymanson.map.ObjectMapper" />
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject" ref="HymansonObjectMapper" />
    <property name="targetMethod" value="configure" />
    <property name="arguments">
        <list>
            <value type="org.codehaus.Hymanson.map.DeserializationConfig.Feature">FAIL_ON_UNKNOWN_PROPERTIES</value>
            <value>false</value>
        </list>
    </property>
</bean>

This can be wired to a RestTemplate like this:

这可以像这样连接到 RestTemplate :

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

It can also be injected directly into the message converters for use with Spring MVC:

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

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

回答by xdebug

For Hymanson 1.9x or below you can ignore unknown properties with object mapper provider

对于 Hymanson 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;
    }
}

For Hymanson 2.x and above you can ignore unknown properties with object mapper provider

对于 Hymanson 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;
    }
}

Jersey classes are not auto-discovered by Spring. Have to register them manually.

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

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

回答by pakman

For newer Hymanson versions (2.x) there are a few changes:

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

<!-- Hymanson Mapper -->
<bean id="HymansonObjectMapper" class="com.fasterxml.Hymanson.databind.ObjectMapper" />
<bean
    class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject" ref="HymansonObjectMapper" />
    <property name="targetMethod" value="configure" />
    <property name="arguments">
        <list>
            <value type="com.fasterxml.Hymanson.databind.DeserializationFeature">FAIL_ON_UNKNOWN_PROPERTIES</value>
            <value>false</value>
        </list>
    </property>
</bean>