3

I have configured @Bean for Jackson2ObjectMapperBuilder where I am setting the DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES property set to false, in order to allow deserialization of JSON objects with unmapped properties. As-

@Bean
public Jackson2ObjectMapperBuilder filteringObjectMapperBuilder() {
    Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
    builder.failOnUnknownProperties(false);
    builder.serializationInclusion(Include.NON_EMPTY);
    builder.annotationIntrospector(new DefaultFilterAnnotationIntrospector("DynamicFieldFilter"));
    return builder;
}

As per spring boot reference documentation Defining a @Bean of type Jackson2ObjectMapperBuilder will allow you to customize both default ObjectMapper and XmlMapper (used in MappingJackson2HttpMessageConverter and MappingJackson2XmlHttpMessageConverter respectively).

But Jackson2ObjectMapperBuilder created in our @Bean configuration class, is not getting used in the message converters. and when a incoming request has unknown properties , it is getting failed.

But When I am configuring Object Mappers as

@Bean
public Jackson2ObjectMapperBuilder filteringObjectMapperBuilder() {
    Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
    builder.failOnUnknownProperties(false);
    builder.serializationInclusion(Include.NON_EMPTY);
    builder.annotationIntrospector(new DefaultFilterAnnotationIntrospector("DynamicFieldFilter"));
    return builder;
}
@Bean
public MappingJackson2HttpMessageConverter jacksonMessageConverter(Jackson2ObjectMapperBuilder builder) {
    return new MappingJackson2HttpMessageConverter(builder.build());
}

@Bean
@ConditionalOnClass(
    name = {"com.fasterxml.jackson.dataformat.xml.XmlMapper"}
)
public MappingJackson2XmlHttpMessageConverter jacksonXmlMessageConverter(Jackson2ObjectMapperBuilder builder) {
    return new MappingJackson2XmlHttpMessageConverter(builder.createXmlMapper(true).build());
}

It is able to accept that incoming request and is not getting failed with unknown properties.

But as per spring boot reference documentation these are not required to configure. Can someone suggest why this is occurring with Spring boot 1.3?

  • 1
    Why not simply set that property in the `application.properties`? `spring.jackson.deserialization.FAIL_ON_UNKNOWN_PROPERTIES=false` instead of trying to hack it into your configuration. – M. Deinum Jul 18 '16 at 06:54
  • 2
    I have tried with setting this in application.properties as well but this is not working. As might be there are some limitation right now in this customization using application.properties. As Spring boot 1.4 is gonna have Jackson2ObjectMapperBuilderCustomizer that will allow us to do what we need to do without messing with the default configuration. But right now it is only working with complete configuration with all Message converters configured but as per documentation these should not be required. – jagmohansharma Jul 18 '16 at 09:11
  • Works for me, so not sure why it wouldn't work for you. When using properties make sure not to override anything for the auto configuration. – M. Deinum Jul 18 '16 at 09:21

0 Answers0