Quick revision
The short answer
We write a @Configuration class implementing WebMvcConfigurer and override four methods: addInterceptors registers the interceptor for /api/** with excludePathPatterns("/api/health"), addCorsMappings allows https://app.example.com on /api/**, addResourceHandlers maps /ui/** to classpath:/ui/ with cache settings, and addFormatters adds a String-to-OrderStatus converter. We must not add @EnableWebMvc, because it imports Spring MVC's full configuration and makes Spring Boot back off its MVC auto-configuration, so the default message converters, static resources and error handling settings are lost.
What to remember
In this answer, we wrote one WebMvcConfigurer that registers an interceptor for /api/** except /api/health, allows CORS from https://app.example.com, serves /ui/** from classpath:/ui/ with a seven-day cache, and converts request strings to OrderStatus. We saw that CORS must also be enabled in Spring Security so preflight requests are not rejected first, and that @EnableWebMvc would make Spring Boot back off its MVC auto-configuration, losing Boot's Jackson setup, static resources and spring.mvc properties.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We write a @Configuration class implementing WebMvcConfigurer and override four methods: addInterceptors registers the interceptor for /api/** with excludePathPatterns("/api/health"), addCorsMappings allows https://app.example.com on /api/**, addResourceHandlers maps /ui/** to classpath:/ui/ with cache settings, and addFormatters adds a String-to-OrderStatus converter. We must not add @EnableWebMvc, because it imports Spring MVC's full configuration and makes Spring Boot back off its MVC auto-configuration, so the default message converters, static resources and error handling settings are lost.
Why this question is asked
WebMvcConfigurer is the everyday way to customise Spring MVC in a Spring Boot service, and almost every service has one. It is also where a single extra annotation can quietly break half of the application. Interviewers ask this to see whether we know the common callbacks, the small details each one needs, and why Boot's auto-configuration and @EnableWebMvc do not mix. In this answer, we will recap what WebMvcConfigurer is, write the four customisations one by one with their results, look at CORS together with Spring Security, and then explain exactly what @EnableWebMvc would switch off.
1A Quick Recap of WebMvcConfigurer
Spring Boot configures Spring MVC for us through auto-configuration: converters, static resources, formatters, content negotiation and error handling. WebMvcConfigurer is a callback interface whose methods all have empty default implementations. Spring calls every WebMvcConfigurer bean while it builds the MVC infrastructure, and each one can add to it. We override only the methods we need.
Because it adds to Boot's setup instead of replacing it, several configurers can coexist: Boot's own, one from a shared library, and ours.
2Writing the Configuration
Here is the whole class. Each method handles one of the four requirements.
| Callback | Adds | Applies to |
|---|---|---|
addInterceptors | TenantInterceptor | /api/** except /api/health |
addCorsMappings | CORS for https://app.example.com | /api/**, GET and POST |
addResourceHandlers | static files with a 7-day cache | /ui/** from classpath:/ui/ |
addFormatters | String to OrderStatus | params, path variables, form fields |
Notice the details. Path patterns use Spring's PathPattern syntax, where ** matches any number of path segments. The resource location ends with a slash; without it, Spring treats ui as a file name prefix and lookups fail. The converter trims and uppercases the input so that paid and PAID both work; an unknown value such as status=lost throws IllegalArgumentException inside conversion, which Spring reports as a 400 type mismatch rather than a 500.
3CORS Together With Spring Security
CORS, cross-origin resource sharing, is how a browser asks whether a page from https://app.example.com may call our API on a different origin. Before a POST with JSON, the browser sends a preflight OPTIONS request and checks the Access-Control-Allow-* response headers.
There is a catch when Spring Security is present. Security's filters run before DispatcherServlet, and the preflight request carries no credentials, so Security may reject it with 401 before Spring MVC can add CORS headers. The browser then reports a CORS error that looks unrelated to security. The fix is to enable CORS in the security chain, which makes Security handle CORS early using a CorsConfigurationSource bean, or, if none is defined and Spring MVC is present, the MVC CORS configuration.
Many teams define a single CorsConfigurationSource bean and let both Security and MVC use it, so the allowed origins live in one place. Check your Spring Security version's documentation for which source it picks up by default.
4Why Not @EnableWebMvc Here
@EnableWebMvc imports DelegatingWebMvcConfiguration, which is Spring MVC's complete configuration class. Spring Boot's WebMvcAutoConfiguration is conditional on that class being absent, because Boot assumes that anyone who adds @EnableWebMvc wants to take full control. So the moment we add it, Boot backs off.
What disappears is quite a lot: Boot's HttpMessageConverters setup, including the Jackson converter built from Boot's configured ObjectMapper, so spring.jackson.* properties stop applying; the default static resource locations such as classpath:/static/; Boot's formatters and the spring.mvc.* properties for date formats, path matching and async timeouts; and some error handling and content negotiation defaults. Our four customisations would still work, but everything else would quietly change.
The rule is simple: in a Spring Boot application, implement WebMvcConfigurer to add things, and use @EnableWebMvc only when we genuinely want to replace Boot's MVC setup completely, which is rare.
5Follow-up questions
How do you change the default Jackson ObjectMapper used by MVC without replacing all converters?
Leave the converters alone and change the mapper that Boot builds. For simple settings, use properties such as spring.jackson.serialization.write-dates-as-timestamps=false or spring.jackson.default-property-inclusion=non_null. For code-level changes, declare a customizer bean, Jackson2ObjectMapperBuilderCustomizer in Spring Boot 3, which adjusts Boot's builder, for example to register a module. Boot then builds one ObjectMapper and its JSON converter uses it. Spring Boot 4 moves to Jackson 3 with a different builder and customizer type, so check your version. Avoid declaring your own ObjectMapper bean unless you want to replace Boot's completely.
Why would an interceptor excluded for /api/health still run for it?
Usually because of a path mismatch, such as the health endpoint actually being /actuator/health, or a trailing slash. Since Spring Framework 6, /api/health/ no longer matches /api/health by default, so the exclusion and the real path must match exactly. A MockMvc test for the excluded path catches this.
What is the difference between addFormatters with a converter and @InitBinder?
addFormatters registers the conversion globally, for every controller. @InitBinder in a controller or @ControllerAdvice customises data binding only for that scope. Global converters suit types used everywhere, like OrderStatus; @InitBinder suits local rules.
6Conclusion
In this answer, we wrote one WebMvcConfigurer that registers an interceptor for /api/** except /api/health, allows CORS from https://app.example.com, serves /ui/** from classpath:/ui/ with a seven-day cache, and converts request strings to OrderStatus. We saw that CORS must also be enabled in Spring Security so preflight requests are not rejected first, and that @EnableWebMvc would make Spring Boot back off its MVC auto-configuration, losing Boot's Jackson setup, static resources and spring.mvc properties.
Open with
I'd write a @Configuration class implementing WebMvcConfigurer and override four methods. In addInterceptors, registry.addInterceptor with addPathPatterns /api/** and excludePathPatterns /api/health. In addCorsMappings, addMapping /api/** with allowedOrigins https://app.example.com, allowedMethods GET and POST, and a maxAge so browsers cache the preflight.
Then, if they want more
In addResourceHandlers, addResourceHandler /ui/** with addResourceLocations classpath:/ui/, with the trailing slash, plus a CacheControl, say seven days public. And in addFormatters, registry.addConverter from String to OrderStatus, trimming and uppercasing, so status=paid binds to PAID and an unknown value gives a 400 type mismatch rather than a 500.
One thing I'd point out with CORS: if Spring Security is on the classpath, the preflight OPTIONS request has no credentials and Security can reject it before MVC adds headers, so I also call http.cors in the security chain, ideally with one shared CorsConfigurationSource. As for @EnableWebMvc: it imports DelegatingWebMvcConfiguration, and Boot's WebMvcAutoConfiguration only applies when that's absent.
So adding it makes Boot back off, and you lose the Jackson converter built from Boot's ObjectMapper, so spring.jackson properties stop working and dates become numbers, the default static locations like classpath:/static, and the spring.mvc properties. In a Boot app, WebMvcConfigurer adds to the setup; @EnableWebMvc replaces it.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Boot 3, Spring Boot 4, Spring Framework 6
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Spring Web MVC
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Write a WebMvcConfigurer for a Spring Boot service that registers an interceptor for...": https://scrivio-interview-library.netlify.app/spring-boot/mvc-architecture/9ea855cbe5/