在 Spring Boot 中使用 WebMvcConfigurer
WebMvcConfigurer
是 Spring MVC 提供的一个扩展接口,用于配置 Spring MVC 的各种功能。在 Spring Boot 应用中,通过实现 WebMvcConfigurer
接口,可以定制和扩展默认的 Spring MVC 配置。以下是对 WebMvcConfigurer
的详细解析及其常见用法。
一、基本概念
WebMvcConfigurer
接口提供了一组回调方法,用于配置 Spring MVC 的各种方面,如视图解析器、拦截器、跨域请求、消息转换器等。通过实现这些方法,可以方便地自定义 MVC 配置。
二、实现 WebMvcConfigurer
创建配置类:
在 Spring Boot 应用中,创建一个配置类并实现WebMvcConfigurer
接口。import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration @EnableWebMvc public class MyWebMvcConfig implements WebMvcConfigurer { // 自定义配置在这里添加 }
配置视图解析器:
通过实现configureViewResolvers
方法,可以自定义视图解析器。import org.springframework.web.servlet.config.annotation.ViewResolverRegistry; @Override public void configureViewResolvers(ViewResolverRegistry registry) { registry.jsp("/WEB-INF/views/", ".jsp"); }
添加拦截器:
通过实现addInterceptors
方法,可以添加拦截器。import org.springframework.web.servlet.config.annotation.InterceptorRegistry; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**"); }
配置跨域请求:
通过实现addCorsMappings
方法,可以配置跨域请求。import org.springframework.web.servlet.config.annotation.CorsRegistry; @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins("http://example.com") .allowedMethods("GET", "POST", "PUT", "DELETE") .allowCredentials(true) .maxAge(3600); }
添加静态资源处理:
通过实现addResourceHandlers
方法,可以配置静态资源的处理。import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/static/**") .addResourceLocations("classpath:/static/"); }
配置消息转换器:
通过实现configureMessageConverters
方法,可以添加或自定义消息转换器。import org.springframework.http.converter.HttpMessageConverter; @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { converters.add(new MyCustomMessageConverter()); }
思维导图
WebMvcConfigurer
视图解析器配置
添加拦截器
跨域请求配置
静态资源处理
消息转换器配置
configureViewResolvers
addInterceptors
addCorsMappings
addResourceHandlers
configureMessageConverters
三、详细示例
下面是一个完整的 WebMvcConfigurer
配置示例,展示了如何配置视图解析器、拦截器、跨域请求、静态资源处理和消息转换器。
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MyWebMvcConfig implements WebMvcConfigurer {
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.jsp("/WEB-INF/views/", ".jsp");
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**");
}
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("http://example.com")
.allowedMethods("GET", "POST", "PUT", "DELETE")
.allowCredentials(true)
.maxAge(3600);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**")
.addResourceLocations("classpath:/static/");
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(new MyCustomMessageConverter());
}
}
总结
通过实现 WebMvcConfigurer
接口,Spring Boot 开发者可以灵活地自定义和扩展 Spring MVC 的配置。无论是视图解析、拦截器、跨域请求处理,还是静态资源和消息转换器配置,WebMvcConfigurer
都提供了一致的接口来实现这些功能。掌握这些配置方法,可以使开发者在 Spring Boot 项目中更加游刃有余地进行各种定制化需求的开发。