How do you globally set Jackson to ignore unknown properties within Spring?
Asked Answered
K

4

68

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

@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:

// 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);

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

Kith answered 15/1, 2013 at 17:36 Comment(0)
K
42

This can be achieved using spring's 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>

This can be wired to a RestTemplate like this:

<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>

It can also be injected directly into the message converters for use with 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>
Kith answered 15/1, 2013 at 17:36 Comment(2)
Not sure what the difference is but I am using com.fasterxml.jackson.databind.ObjectMapper and therefore had to use com.fasterxml.jackson.databind.DeserializationFeature. Slightly different if you look very closely at the jacksonObjectMapper bean and its associated MethodInvokingFactoryBean.Wilkie
For Jackson v2 defined message converted should be: org.springframework.http.converter.json.MappingJackson2HttpMessageConverterColicweed
R
61

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

@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 jackson 2.x and above you can ignore unknown properties with object mapper provider

@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.

public class JerseyConfig extends ResourceConfig {
    public JerseyConfig() {
        register(JerseyObjectMapperProvider.class);
    }
}
Rhadamanthus answered 5/4, 2014 at 0:4 Comment(2)
note: to use this Jersey config with Spring Boot you also need to register is manually: /** * Jersey classes are not auto-discovered by Spring. Have to register them manually. */ @Named public class JerseyConfig extends ResourceConfig { public JerseyConfig() { this.register(JerseyObjectMapperProvider.class); } }Lindalindahl
Thanks for the suggestion @LindalindahlRhadamanthus
K
42

This can be achieved using spring's 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>

This can be wired to a RestTemplate like this:

<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>

It can also be injected directly into the message converters for use with 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>
Kith answered 15/1, 2013 at 17:36 Comment(2)
Not sure what the difference is but I am using com.fasterxml.jackson.databind.ObjectMapper and therefore had to use com.fasterxml.jackson.databind.DeserializationFeature. Slightly different if you look very closely at the jacksonObjectMapper bean and its associated MethodInvokingFactoryBean.Wilkie
For Jackson v2 defined message converted should be: org.springframework.http.converter.json.MappingJackson2HttpMessageConverterColicweed
T
21

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

<!-- 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>
Titoism answered 24/10, 2013 at 0:8 Comment(1)
In Java code, it is like this: objectMapper.configure(com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);Kwei
D
0

For those who are using FeignClient. Just define a custom configuration in Feign, and provide Jacksondecoder.

sample below

@FeignClient(value = "name", url = "sample.url", configuration = XyzFeignClientConfig.class)

And in your feign config just return bean of JacksonDecoder.

public class XyzFeignClientConfig {
    @Bean
    public Decoder decoder() {
        return new JacksonDecoder();
    }
}
Dallis answered 20/4, 2023 at 16:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.