WebMvcConfigurer — customizing MVC. — Cracked Java
// Spring Framework & Spring Boot · Spring MVC & REST
SeniorCoding

WebMvcConfigurer — customizing MVC.

WebMvcConfigurer is the supported, additive way to customize Spring MVC — you implement the callbacks you care about and Spring merges your tweaks into the default configuration. It's the right answer to "how do I change MVC behavior" precisely because it adds to the defaults rather than replacing them.

The shape

Declare a @Configuration bean implementing WebMvcConfigurer and override only the hooks you need (all have empty default methods):

@Configuration
class WebConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new AuthInterceptor()).addPathPatterns("/api/**");
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/api/**").allowedOrigins("https://app.example.com");
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(new MyCustomConverter()); // or extendMessageConverters to keep defaults
    }

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new StringToOrderStatusConverter());
    }
}

The common hooks

  • addInterceptors — register HandlerInterceptors with path patterns.
  • addCorsMappings — declarative CORS.
  • addFormatters — custom Converter/Formatter for type conversion in binding.
  • configureContentNegotiation — header/parameter strategies (see content negotiation).
  • configureMessageConverters / extendMessageConverters — replace vs augment converters.
  • addResourceHandlers — static resource mapping.
  • addArgumentResolvers / addReturnValueHandlers — custom method argument/return support.
  • configureViewResolvers, addViewControllers.

The crucial distinction: additive vs replacing

Implementing WebMvcConfigurer keeps Spring Boot's auto-configured MVC defaults and layers your changes on top. Adding @EnableWebMvc switches off Boot's MVC auto-configuration and gives you the bare framework defaults — rarely what you want in a Boot app. Likewise, configureMessageConverters replaces the default converter list while extendMessageConverters appends to it.

Mark your status