Spring MVC 4.3.29 and Java 8 (current platform constraints), and mostly XML configuration, except for some Controller classes that are annotation-scanned.
In short, I want to get the ObjectMapper
instance being used automatically by Spring JSON deserialization, and I want to set its FAIL_ON_UNKNOWN_PROPERTIES
back to true
.
I see several related questions, but all the examples seem to be Spring Boot and/or Java configuration. And none of the suggested @Autowired
beans (Mapper, Builder, etc.) have any values at all in my WebSphere environment.
Hopefully I'm just missing some simple glue somewhere.
Edit: Bah, I thought I had it with this:
@Configuration
public class CustomWebConfiguration extends WebMvcConfigurerAdapter {
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
for (HttpMessageConverter<?> converter : converters) {
if (converter instanceof MappingJackson2HttpMessageConverter) {
((MappingJackson2HttpMessageConverter) converter).getObjectMapper().
enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
break;
}
}
}
}
And with my debugger I can see that it is being hit and changing the expected flag. But when used, the behavior is not in effect. I no longer have any XML overrides in place, but I do still have the "master" <mvc:annotation-driven/>
there. I wonder if those are confusing each other...
Ok, yes, this works as long as it's combined with @EnableWebMvc
rather than <mvc:annotation-driven/>
:
@EnableWebMvc
@Configuration
public class CustomWebConfiguration implements WebMvcConfigurer {
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
for (HttpMessageConverter<?> converter : converters) {
if (converter instanceof MappingJackson2HttpMessageConverter) {
((MappingJackson2HttpMessageConverter) converter).getObjectMapper().
enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
break;
}
}
}
}