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.