Backend Development 8 min read

Extending and Fully Controlling Spring MVC in Spring Boot

This article explains how to extend Spring MVC in Spring Boot by implementing WebMvcConfigurer, how the default auto‑configuration works, and why using @EnableWebMvc fully takes over MVC configuration, while also providing code examples and resource links for deeper learning.

Code Ape Tech Column
Code Ape Tech Column
Code Ape Tech Column
Extending and Fully Controlling Spring MVC in Spring Boot

Spring Boot greatly simplifies MVC configuration by providing sensible defaults, but sometimes custom MVC behavior is required. This guide shows how to extend MVC without disabling the auto‑configuration and how to fully take over MVC when necessary.

Spring Boot version : The examples are based on 2.3.4.RELEASE .

How to extend MVC : Ensure the configuration class is not annotated with @EnableWebMvc and does not extend WebMvcConfigurationSupport . Then create a configuration class annotated with @Configuration and implement the WebMvcConfigurer interface. The interface provides methods for customizing interceptors, view resolvers, resource handlers, etc.

Create a class with @Configuration .

Implement WebMvcConfigurer and override the needed methods.

Prior to Spring Boot 2.3.4, WebMvcConfigurerAdapter could be extended, but it is deprecated in this version:

@Deprecated
public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer {}

Example – Adding an interceptor :

/**
 * MVC extension configuration class implementing WebMvcConfigurer
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private RepeatSubmitInterceptor repeatSubmitInterceptor;

    /**
     * Register custom interceptor
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(repeatSubmitInterceptor).excludePathPatterns("/error");
    }
}

Even without any custom configuration, Spring Boot can run MVC features because the spring-boot-starter-web starter imports an auto‑configuration class WebMvcAutoConfiguration , which registers default view resolvers, resource handlers, etc.

The auto‑configuration class contains a nested class WebMvcAutoConfigurationAdapter that implements WebMvcConfigurer , providing the default MVC setup before the application starts.

Fully taking over MVC (not recommended) : Adding @EnableWebMvc to a configuration class imports DelegatingWebMvcConfiguration , which extends WebMvcConfigurationSupport . This disables the default auto‑configuration because WebMvcAutoConfiguration is conditional on the absence of a WebMvcConfigurationSupport bean.

@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
    @Autowired
    private RepeatSubmitInterceptor repeatSubmitInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // Add interceptor
        registry.addInterceptor(repeatSubmitInterceptor).excludePathPatterns("/error");
    }
}

The @ConditionalOnMissingBean(WebMvcConfigurationSupport.class) annotation on WebMvcAutoConfiguration prevents it from loading when @EnableWebMvc is present, thus the starter loses its effect.

Additional resources : The author provides free video tutorials for beginner and advanced Spring Boot topics, accessible by replying with keywords “Spring Boot初级” or “Spring Boot高级” to the public account. Links to previous articles covering filters, interceptors, logging, and other Spring Boot features are also listed.

Conclusion : Extending MVC via WebMvcConfigurer is straightforward and recommended, while fully taking over MVC with @EnableWebMvc disables many convenient auto‑configurations and is generally discouraged.

MVCbackend developmentSpring BootEnableWebMvcWebMvcConfigurer
Code Ape Tech Column
Written by

Code Ape Tech Column

Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn

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.