7

I have read Spring boot doc (http://projects.spring.io/spring-boot/docs/docs/howto.html#message.converters ) and its mentioned that if you provide your own JacksonConvertor, it will override the default one. But I guess its not working with the below code.

What I want to do is to set DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES of the object mapper to false.

@EnableAutoConfiguration
@ComponentScan("com.hjh")
@Configuration
public class App {

    @Bean
    @Primary
    public MappingJackson2HttpMessageConverter jacksonConvertor(){
        MappingJackson2HttpMessageConverter convertor= new MappingJackson2HttpMessageConverter();
        ObjectMapper mapper = new ObjectMapper();
        mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        convertor.setObjectMapper(mapper);
        return convertor;
    }

    public static void main(String[] args) throws Exception {
        ApplicationContext ctx =   SpringApplication.run(App.class, args);

    }

Could any one please point out what am i doing wrong here? As it keeps on trying to bind the unknown prop from the request. If I remove the unknown prop, it all goes well

hellojava
  • 4,904
  • 9
  • 30
  • 38
  • 1
    What version of Boot is this (I recommend upgrading to the latest snapshot)? Also please post a stack trace or we don't really know what went wrong. – Dave Syer Jan 21 '14 at 15:58
  • Upgrading the version to 0.5.0.M7 from 0.5.0.M6. Everything works as expected. Thanks Dave – hellojava Jan 21 '14 at 17:44

2 Answers2

25

Starting from Spring Boot 1.2.0.RC2 FAIL_ON_UNKNOWN_PROPERTIES is set to false by default. It can be turned back on by adding property to application.properties:

spring.jackson.deserialization.fail-on-unknown-properties=true
Maciej Walkowiak
  • 12,372
  • 59
  • 63
  • I tried making this change to my Spring Boot application (1.1.9 RELEASE) and it didn't change the behavior. Adding the Primary Bean to my Configuration class (as shown in the OPs posting), however, did fix my problem – IcedDante Dec 28 '14 at 20:11
  • 3
    Same here. I have a unit tests that expects a 400 error when POSTing to a rest endpoint with a field that it doesn't recognize. When upgrading my app from Spring Boot 1.2.0.M2 to 1.2.0.RC2, this test starts to fail. Adding the property spring.jackson.deserialization.fail-on-unknown-properties=true to application.properties also has absolutely no effect. – Kevin M Feb 03 '15 at 20:57
  • 1
    i have found that setting the "spring.jackson.deserialization.fail-on-unknown-properties" or any other jackson property via the spring config file "application.properties" will only affect the ObjectMapper if it was defined as a Bean. So if you are creating your own object mapper via e.g., "new ObjectMapper()" then the properties will have no effect. – abdel Jul 30 '18 at 14:30
1

I recommend upgrading to the latest snapshot. "1.0.0.BUILD-SNAPSHOT" is the new latest (RC1 to follow later today hopefully).

Dave Syer
  • 56,583
  • 10
  • 155
  • 143
  • Can I know about the timeline for the final release schedule as well? – hellojava Jan 21 '14 at 17:50
  • Normally we'd expect to have 1 or 2 release candidates separated by a week or two each, then a final release. Could be more and could be longer. It all depends on how many issues are reported. – Dave Syer Jan 21 '14 at 17:56