8

I am using spring.jackson.deserialization.FAIL_ON_UNKNOWN_PROPERTIES=true in the application.properties to make deserialization fail on unknown properties but its not working.

I even tried using :

@Bean
ObjectMapper objectMapper() {
  return Jackson2ObjectMapperBuilder
        .json()
        .featuresToEnable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
        .build();
}

But this also didn't work. What am I missing?

Littm
  • 4,923
  • 4
  • 30
  • 38
Kiba
  • 399
  • 1
  • 4
  • 16
  • Can you post your code & the data which objects you are trying to deserialize? – Vasu Mar 18 '17 at 12:36
  • Does it work if you define `Jackson2ObjectMapperBuilder` as in this question: https://stackoverflow.com/questions/34545997/put-and-post-fail-on-unknown-properties-spring-different-behavior ? – Infinity Mar 18 '17 at 13:06
  • @Infinity Thanks for quick reply. I tried that also but didn't work It's a plain project with spring-boot web dependency and a PUT request method to update user (right i'm not hitting database,i'm just logging update request to test method ) and a Plain User Model without any additional annotations. – Kiba Mar 18 '17 at 13:20
  • So does it work with POST requests? – Infinity Mar 18 '17 at 13:32
  • @Infinity No, it's not working for POST also. – Kiba Mar 18 '17 at 13:41
  • @Infinity It's working now, i cleared the m2 cache and rebuild it, maybe it was a version issue. Thanks for your quick replies. I'll check later what was causing it. – Kiba Mar 18 '17 at 17:40

2 Answers2

12

With Spring boot, by default, unknown properties are ignored during deserialization. In order not to ignore these properties, you can :

  • Add spring.jackson.deserialization.FAIL_ON_UNKNOWN_PROPERTIES=true in the application.properties
  • Use this bean :

    @Bean
    public ObjectMapper objectMapper() {
        return Jackson2ObjectMapperBuilder
        .json()
        .featuresToEnable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
        .build();
    }
    
  • Use this bean :

    @Bean 
    public Jackson2ObjectMapperBuilder objectMapperBuilder(){
        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
        builder.failOnUnknownProperties(true);
        return builder;
    }
    

I have tested these solutions and it works so there is another problem in your code. Please, can you post your code as ask by javaguy?

Chacko
  • 1,506
  • 1
  • 20
  • 42
Sébastien Temprado
  • 1,413
  • 4
  • 18
  • 29
  • Thanks for quick reply. It's working now, i cleared the m2 cache and rebuild it, maybe it was a version issue. I'll check later what was causing it. I'm not able to mark both your and Fırat guy's reply as an answer. But thanks for quick reply :). – Kiba Mar 18 '17 at 17:45
  • This answer is more complete, it provides multiple solutions, and in my case (relying on Spring's auto-configuration) Spring was instantiating multiple `ObjectMapper`s and only one of them used the property that I set in `application.properties`. Utilizing the third option from this answer worked for me (although I had to add a `@Bean` for `MappingJackson2HttpMessageConverter` that utilized my custom `Jackson2ObjectMapperBuilder` for it to work). – Tim Klein Jan 03 '19 at 15:36
  • If the setting in the application.yaml does not have the desired effect, then check your code. You may have defined an object mapper somewhere. This overwrites the setting behavior by default! – Sma Ma Nov 22 '22 at 11:24
9

FAIL_ON_UNKNOWN_PROPERTIES option is true by default according to Jackson Documentation.

If you want to disable this setting you may add this option to application.properties.

spring.jackson.deserialization.fail-on-unknown-properties = false

But in default settings it works as expected. So no need for any setting.

This is one file spring boot application:

@RestController
@SpringBootApplication
public class TestOptionApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestOptionApplication.class, args);
    }

    @PostMapping("/test")
    public void formTest(@RequestBody final HelloForm form) {
    }

    public static class HelloForm {

        private String name;

        public String getName() { return name; }

        public void setName(final String name) { this.name = name; }
    }
}

This is the integration test for testing rejection on unknown properties.

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestOptionApplicationTest {

    @Autowired
    private WebApplicationContext context;

    @Test
    public void testFailOnUnknownPropertiesOption() throws Exception {

        final String text = "{\"name\": \"test\", \"title\": \"test\"}";

        MockMvcBuilders
            .webAppContextSetup(this.context)
            .build()
            .perform(post("/test").contentType(MediaType.APPLICATION_JSON).content(text))
            .andExpect(status().isBadRequest());
    }
}

title property is not defined. So controller sends BadRequest.

Fırat Küçük
  • 5,613
  • 2
  • 50
  • 53
  • 1
    Thanks for quick reply. It's working now, i cleared the m2 cache and rebuild it, maybe it was a version issue. I'll check later what was causing it. I'll mark this as an answer. – Kiba Mar 18 '17 at 17:42
  • I tried but it workd with uppercase: spring.jackson.deserialization.FAIL_ON_UNKNOWN_PROPERTIES. – Erikson Murrugarra Dec 10 '19 at 23:26