继承WebMvcConfigurationSupport导致Spring Boot MVC自动配置被阻断,拦截器无法注册的问题分析与解决
WebMvcConfigurationSupport Let's break down the root cause first, then walk through practical fixes.
The Core Issue: WebMvcConfigurationSupport Disables Spring Boot's Web Auto-Configuration
Spring Boot’s WebMvcAutoConfiguration—which handles default MVC setup, including enabling auto-configured interceptors to register—has a critical conditional check: @ConditionalOnMissingBean(WebMvcConfigurationSupport.class).
In plain terms: If any class in your application inherits from WebMvcConfigurationSupport and is marked as @Configuration, Spring Boot completely skips loading its default WebMvcAutoConfiguration.
Your interceptor library relies on this auto-configuration mechanism to register its interceptors automatically. Since WebMvcAutoConfiguration isn’t running, the library’s auto-config beans (and your breakpoints!) never execute. That’s why deleting the JacksonHttpMessageConfig class fixes the problem—it lets Spring Boot’s default configuration kick back in.
Fixes to Resolve the Problem
1. Replace WebMvcConfigurationSupport with WebMvcConfigurer (Recommended)
Instead of extending WebMvcConfigurationSupport, implement the WebMvcConfigurer interface. This lets you customize MVC settings (like message converters) without disabling Spring Boot’s auto-configuration. Here’s how to adjust your class:
@Configuration public class JacksonHttpMessageConfig implements WebMvcConfigurer { @Bean public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() { MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter(); ObjectMapper om = new ObjectMapper(); om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); om.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true); jsonConverter.setObjectMapper(om); return jsonConverter; } @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { converters.add(mappingJackson2HttpMessageConverter()); // No need to call super.addDefaultHttpMessageConverters() here— // WebMvcConfigurer's default logic preserves default converters, // and adding your custom converter first gives it higher priority } }
This way, your custom Jackson converter is registered, and Spring Boot’s auto-configuration (including your interceptor library’s setup) runs normally.
2. Manually Register the Interceptor (If You Must Keep WebMvcConfigurationSupport)
If you have a specific reason to stick with WebMvcConfigurationSupport, you’ll need to manually register the interceptor from your library. First, ensure the interceptor is exposed as a bean in the library, then inject it into your config and add it via addInterceptors:
@Configuration public class JacksonHttpMessageConfig extends WebMvcConfigurationSupport { // Inject the interceptor bean from your library private final YourInterceptorClass yourInterceptor; public JacksonHttpMessageConfig(YourInterceptorClass yourInterceptor) { this.yourInterceptor = yourInterceptor; } // ... your existing mappingJackson2HttpMessageConverter() method ... @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { converters.add(mappingJackson2HttpMessageConverter()); super.addDefaultHttpMessageConverters(converters); } // Add this method to register the interceptor @Override protected void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(yourInterceptor); // If your interceptor needs specific path patterns, add them here: // registry.addInterceptor(yourInterceptor).addPathPatterns("/api/**"); super.addInterceptors(registry); } }
Note: You’ll need to know the exact class name of the interceptor from your library to inject it. This approach loses the "auto-config" convenience but works if you can’t switch to WebMvcConfigurer.
3. Avoid @EnableWebMvc (If You’re Using It)
A quick heads-up: If you ever use @EnableWebMvc in your project, it does the same thing as extending WebMvcConfigurationSupport—it disables Spring Boot’s auto-configuration. Double-check you’re not using this annotation accidentally.
内容的提问来源于stack exchange,提问作者steelcityamir

