Backend Development 8 min read

Mastering Spring MVC Auto-Configuration in Spring Boot 2.4.12

This guide explains how Spring Boot 2.4.12 automatically configures Spring MVC components such as view resolvers, converters, and message handlers, and shows how to customize them via WebMvcConfigurer, WebMvcRegistrations, HttpMessageConverters, and @JsonComponent, including code examples for full control over the Web MVC setup.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Mastering Spring MVC Auto-Configuration in Spring Boot 2.4.12

Environment: Spring Boot 2.4.12

Spring MVC Auto-Configuration

Spring Boot provides auto‑configuration for Spring MVC that works well with most applications. The auto‑configuration adds the following features on top of Spring’s default settings:

Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.

Support for serving static resources, including WebJars.

Automatic registration of Converter , GenericConverter and Formatter beans.

Support for HttpMessageConverters .

Automatic registration of MessageCodesResolver .

Support for a static index.html .

Automatic use of a ConfigurableWebBindingInitializer bean.

If you want to keep the Spring Boot MVC defaults while adding custom MVC components (interceptors, formatters, view controllers, etc.), you can create a WebMvcConfigurer bean annotated with @Configuration , but you do not need to add @EnableWebMvc .

To provide custom instances of RequestMappingHandlerMapping , RequestMappingHandlerAdapter or ExceptionHandlerExceptionResolver while still using Spring Boot’s MVC auto‑configuration, declare a bean of type WebMvcRegistrations and return the customized components.

Example interface:

<code>public interface WebMvcRegistrations {
    default RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
        return null;
    }
    default RequestMappingHandlerAdapter getRequestMappingHandlerAdapter() {
        return null;
    }
    default ExceptionHandlerExceptionResolver getExceptionHandlerExceptionResolver() {
        return null;
    }
}
</code>

Spring Boot injects the custom WebMvcRegistrations into its internal configuration:

<code>@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(WebProperties.class)
public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration implements ResourceLoaderAware {
    private final WebMvcRegistrations mvcRegistrations;
    public EnableWebMvcConfiguration(ObjectProvider<WebMvcRegistrations> mvcRegistrations) {
        this.mvcRegistrations = mvcRegistrations.getIfUnique();
    }
    // ... other methods ...
    @Override
    protected RequestMappingHandlerAdapter createRequestMappingHandlerAdapter() {
        if (this.mvcRegistrations != null) {
            RequestMappingHandlerAdapter adapter = this.mvcRegistrations.getRequestMappingHandlerAdapter();
            if (adapter != null) {
                return adapter;
            }
        }
        return super.createRequestMappingHandlerAdapter();
    }
}
</code>

To take complete control of Web MVC configuration you can add a @Configuration class annotated with @EnableWebMvc, or create a @Configuration class that extends DelegatingWebMvcConfiguration .

To customize the ConversionService used by Spring MVC, provide a WebMvcConfigurer bean with an addFormatters method, or delegate to the static methods of ApplicationConversionService .

HttpMessageConverters

Spring MVC uses the HttpMessageConverter interface to convert HTTP requests and responses. By default, objects are automatically converted to JSON (via Jackson) or XML (via Jackson XML or JAXB). Strings are encoded in UTF‑8.

To add or customize converters, define a bean of type HttpMessageConverters :

<code>@Configuration(proxyBeanMethods = false)
public class MyConfiguration {
    @Bean
    public HttpMessageConverters customConverters() {
        HttpMessageConverter<?> additional = ...;
        HttpMessageConverter<?> another = ...;
        return new HttpMessageConverters(additional, another);
    }
}
</code>

The auto‑configuration picks up these custom converters through WebMvcAutoConfigurationAdapter :

<code>public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer {
    private final ObjectProvider<HttpMessageConverters> messageConvertersProvider;
    public WebMvcAutoConfigurationAdapter(ObjectProvider<HttpMessageConverters> messageConvertersProvider) {
        this.messageConvertersProvider = messageConvertersProvider;
    }
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        this.messageConvertersProvider.ifAvailable(customConverters ->
            converters.addAll(customConverters.getConverters()));
    }
}
</code>

Custom HttpMessageConverter beans are thus added to the container.

Custom JSON Serialization and Deserialization

If you use Jackson for JSON, you can create your own JsonSerializer and JsonDeserializer classes. Register them with the @JsonComponent annotation, which is meta‑annotated with @Component, so normal component scanning applies.

<code>@JsonComponent
public class Example {
    public static class Serializer extends JsonSerializer<SomeObject> {
        // ...
    }
    public static class Deserializer extends JsonDeserializer<SomeObject> {
        // ...
    }
}
</code>

All @JsonComponent beans in the ApplicationContext are automatically registered with Jackson. Spring Boot also provides base classes JsonObjectSerializer and JsonObjectDeserializer as convenient alternatives.

End of section.

Spring BootSpring MVCAuto‑ConfigurationJSON serializationWebMvcConfigurerHttpMessageConverters
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.