当前位置: 首页 > news >正文

【springboot】 WebMvcConfigurer的使用

org.springframework.web.servlet.config.annotation.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());
}

import org.springframework.http.converter.HttpMessageConverter;

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(new MyCustomMessageConverter());
}

import org.springframework.http.converter.HttpMessageConverter;

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(new MyCustomMessageConverter());
}

三、详细示例
下面是一个完整的 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());
}

}

http://www.jsqmd.com/news/46935/

相关文章:

  • 2025年11月21日
  • 实用指南:一文搞懂 DeepSeek API:兼容 OpenAI 接口的智能对话模型调用指南
  • 形容词Test
  • James Watsons
  • 2025年11月江苏/徐州vr设备、vr体验馆、5d影院、9d影院、拓普互动厂家推荐榜:拓普互动强势登顶
  • 计算机网络:物理层 - 实践
  • 2025年11月中国/江苏/徐州vr设备、vr体验馆、5d影院、9d影院、拓普互动厂家TOP10:拓普互动领跑榜单
  • MongoDB 平替新方案:金仓多模数据库驱动电子证照国产化落地 - 详解
  • 高考数学如何有效提分?一位家长关于分阶段选择数学老师的心得体会
  • 最佳加密货币钱包选择指南:企业级安全与功能全解析
  • 让 Maven 能找到本地 JAR 而无需把它上传到公共仓库:
  • Windows Server 2019 中文版、英文版下载 (2025 年 11 月更新)
  • TSMaster + SkyEye:更早、更快、更全面的数字化验证正在成为行业共识
  • 2025年11月中国/江苏/徐州MPP电力管,PE管,喷灌机管,建筑排水管,虹吸管综合评测与权威推荐榜单
  • Windows 11 25H2 | 24H2 中文版、英文版 (x64、ARM64) 下载 (2025 年 11 月更新)
  • python-oop-1
  • 留学中介文书原创占比谁最大?业内锐评的专业榜单来了!
  • Windows 7 Windows Server 2008 R2 简体中文版下载 (2025 年 11 月更新)
  • 2025 年国内水质采样器厂家市场排名与品牌影响力分析报告
  • Windows Server 2016 中文版、英文版下载 (2025 年 11 月更新)
  • 2025年11月中国/江苏/徐州MPP电力管,PE管,喷灌机管,建筑排水管,虹吸管权威推荐榜:评测与选购指南
  • 留学中介文书结构榜!申请院校哪家机构逻辑最清晰?
  • 2025年包头家政服务标杆企业最新推荐:信达家政,保洁|保姆|开荒|月嫂|护工一体化服务新标准
  • 每日反思(2025年11月21日)
  • 2025 年国内空气站智慧站房厂家市场排名与品牌影响力分析报告
  • ARC 杂题乱做 #1
  • 工作小结——Qwen2-7B-Instruct调用
  • AI元人文:不完美中的前行——挑战回应、技术路径与治理框架
  • 最终留学中介文书口碑对决!哪家用户评价最炸裂?
  • 留学中介这份文书终极排行快看!哪家最值得信任?