1

I find out solution for Jackson customization (Jackson2ObjectMapperBuilder enable field visibility ANY) by trial-and-error method.

And there are some question about it:

  • Why doesn't it works until I remove @EnableWebMvc?
  • Why @Bean doesn't work if I put it to @Configuration class extended from WebMvcConfigurerAdapter?

I put breakpoint to main ObjectMapper, MappingJackson2HttpMessageConverter constructors and found in simple spring boot web mvc application

ObjectMapper created 9 times!

MappingJackson2HttpMessageConverter created 8 times!

Why? and why no ambiguous during @Autowired for ObjectMapper?

All this question could be express in single one: are there any docs to get known how all of this configuration works together?

Community
  • 1
  • 1
Grigory Kislin
  • 16,647
  • 10
  • 125
  • 197
  • If you add `@EnableWebMvc` you disable auto configuration from spring boot because it already detects your own configuration. If you add it as a bean it overrides again the default. When doing it like you do, you basically have a `BeanPostProcessor` which further modifies the bean created somewhere else. – M. Deinum Feb 11 '16 at 08:07

2 Answers2

3

Why doesn't it works until I remove @EnableWebMvc

When you use @EnableWebMvc you are telling Spring Boot that you want to take complete control of configuring Spring MVC. This is covered in the Spring Boot reference documentation where it says "the easiest way to take complete control over MVC configuration is to provide your own @Configuration with the @EnableWebMvc annotation. This will leave all MVC configuration in your hands."

I put breakpoint to main ObjectMapper, MappingJackson2HttpMessageConverter constructors and found in simple spring boot web mvc application

ObjectMapper created 9 times!

MappingJackson2HttpMessageConverter created 8 times!

Why?

A number of default ObjectMapper and MappingJackson2HttpMessageConverter instances are created by Spring MVC (and others). The default instances are then customised or replaced by Spring Boot as required.

And why no ambiguous during @Autowired for ObjectMapper?

Autowiring only cares about instances of a class that are Spring beans. If an instance has been created and has not be exposed as a Spring bean autowiring will know nothing about it.

All this question could be express in single one: are there any docs to get known how all of this configuration works together?

The Spring Boot reference guide covers a lot of this. In addition to the section on @EnableWebMvc linked to above, the appendix that lists all of the auto-configuration classes may also be of interest. Lastly, for understanding how things like @Autowired work, I would recommend reading the Spring Framework reference guide.

Community
  • 1
  • 1
Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
  • Andy, thank you for detail answer and documents reference. I'd like to have some document similar to picture of "spring container bean lifecycle" (http://piyushramani8.blogspot.ru/2013/07/spring-container-bean-life-cycle.html) if it is possible, to have a look from top. Curious, but jackson serialized fields only when I REMOVE `@EnableWebMvc` above `@Configuration public class MvcConfiguration extends WebMvcConfigurerAdapter`. But work OK with or without it in my own `@Configuration` class. What is the difference in what class I annotated by `@EnableWebMvc`? – Grigory Kislin Feb 11 '16 at 11:10
0

You could take a look at WebMvcAutoConfiguration.java

james_s_tayler
  • 1,823
  • 1
  • 15
  • 20