66

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?

jnrcorp
  • 1,905
  • 1
  • 18
  • 25

4 Answers4

60

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);
    }
}
xdebug
  • 1,178
  • 12
  • 16
  • 1
    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); } }` – Alex Jun 24 '16 at 22:23
  • Thanks for the suggestion @Alex – xdebug May 26 '17 at 08:04
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>
jnrcorp
  • 1,905
  • 1
  • 18
  • 25
  • 1
    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. – Nick Foote May 15 '13 at 14:13
  • 1
    For Jackson v2 defined message converted should be: org.springframework.http.converter.json.MappingJackson2HttpMessageConverter – svlada May 14 '15 at 09:48
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>
pakman
  • 1,676
  • 3
  • 23
  • 41
  • 5
    In Java code, it is like this: objectMapper.configure(com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); – Jerry Tian Oct 09 '15 at 10:30
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();
    }
}