首页 > 其他分享 >springboot~WebMvcConfigurer详解

springboot~WebMvcConfigurer详解

时间:2023-02-01 17:36:03浏览次数:60  
标签:var1 springboot void classpath 详解 registry WebMvcConfigurer addResourceLocations

1. 前言

WebMvcConfigurer配置类其实是Spring内部的一种配置方式,采用JavaBean的形式来代替传统的xml配置文件形式进行针对框架个性化定制,可以自定义一些Handler,Interceptor,ViewResolver,MessageConverter。基于java-based方式的spring mvc配置,需要创建一个配置类并实现WebMvcConfigurer 接口;

在Spring Boot 1.5版本都是靠重写WebMvcConfigurerAdapter的方法来添加自定义拦截器,消息转换器等。SpringBoot 2.0 后,该类被标记为@Deprecated(弃用)。官方推荐直接实现WebMvcConfigurer或者直接继承WebMvcConfigurationSupport,方式一实现WebMvcConfigurer接口(推荐),方式二继承WebMvcConfigurationSupport类,具体实现可看这篇文章。https://blog.csdn.net/fmwind/article/details/82832758

冲突问题

SpringBoot添加Interceptor后addInterceptors方法不执行,拦截器不生效

原因其实很简单,因为代码中有【WebMvcConfigurationSupport】的继承类,SpringBoot会判断,如果有【WebMvcConfigurationSupport】就不会加载【WebMvcConfigurer】。

2. WebMvcConfigurer接口说明

2.1:原码

 public interface WebMvcConfigurer {
    void configurePathMatch(PathMatchConfigurer var1);
 
    void configureContentNegotiation(ContentNegotiationConfigurer var1);
 
    void configureAsyncSupport(AsyncSupportConfigurer var1);
 
    void configureDefaultServletHandling(DefaultServletHandlerConfigurer var1);
 
    void addFormatters(FormatterRegistry var1);
 
    void addInterceptors(InterceptorRegistry var1);
 
    void addResourceHandlers(ResourceHandlerRegistry var1);
 
    void addCorsMappings(CorsRegistry var1);
 
    void addViewControllers(ViewControllerRegistry var1);
 
    void configureViewResolvers(ViewResolverRegistry var1);
 
    void addArgumentResolvers(List<HandlerMethodArgumentResolver> var1);
 
    void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> var1);
 
    void configureMessageConverters(List<HttpMessageConverter<?>> var1);
 
    void extendMessageConverters(List<HttpMessageConverter<?>> var1);
 
    void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> var1);
 
    void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> var1);
 
    Validator getValidator();
 
    MessageCodesResolver getMessageCodesResolver();
}

2.2:常用的方法:

 /* 拦截器配置 */
void addInterceptors(InterceptorRegistry var1);
/* 视图跳转控制器 */
void addViewControllers(ViewControllerRegistry registry);
/**
     *静态资源处理
**/
void addResourceHandlers(ResourceHandlerRegistry registry);
/* 默认静态资源处理器 */
void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer);
/**
     * 这里配置视图解析器
 **/
void configureViewResolvers(ViewResolverRegistry registry);
/* 配置内容裁决的一些选项*/
void configureContentNegotiation(ContentNegotiationConfigurer configurer);
/** 解决跨域问题 **/
public void addCorsMappings(CorsRegistry registry) ;

2.3 addInterceptors:拦截器

addInterceptor:需要一个实现HandlerInterceptor接口的拦截器实例
addPathPatterns:用于设置拦截器的过滤路径规则;addPathPatterns("/**")对所有请求都拦截
excludePathPatterns:用于设置不需要拦截的过滤规则
拦截器主要用途:进行用户登录状态的拦截,日志的拦截等。

@Override
public void addInterceptors(InterceptorRegistry registry) {
    super.addInterceptors(registry);
    registry.addInterceptor(new TestInterceptor()).addPathPatterns("/**").excludePathPatterns("/emp/toLogin","/emp/login","/js/**","/css/**","/images/**");
}

2.4 addViewControllers:页面跳转, 比如登录跳转或错误页面跳转

以前写SpringMVC的时候,如果需要访问一个页面,必须要写Controller类,然后再写一个方法跳转到页面,感觉好麻烦,其实重写WebMvcConfigurer中的addViewControllers方法即可达到效果了
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/toLogin").setViewName("login");
registry.addViewController("/403").setViewName("/403");
registry.addViewController("/404").setViewName("/404");
}

值的指出的是,在这里重写addViewControllers方法,并不会覆盖WebMvcAutoConfiguration(Springboot自动配置)中的addViewControllers(在此方法中,Spring Boot将“/”映射至index.html),这也就意味着自己的配置和Spring Boot的自动配置同时有效,这也是我们推荐添加自己的MVC配置的方式。

2.5 addResourceHandlers:静态资源

比如,我们想自定义静态资源映射目录的话,只需重写addResourceHandlers方法即可。
注:如果继承WebMvcConfigurationSupport类实现配置时必须要重写该方法,具体见其它文章

@Configuration
public class MyWebMvcConfigurerAdapter implements WebMvcConfigurer {
    /**
     * 配置静态访问资源
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/resources")
                .addResourceLocations("classpath:/static")
                .addResourceLocations("classpath:/templates")
                .addResourceLocations("classpath:/cert")
                .addResourceLocations("classpath:/ueditor")
                .addResourceLocations("classpath:/shiro");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");

        registry.addResourceHandler("/workflow/**").addResourceLocations("classpath:/static/workflow/");
        super.addResourceHandlers(registry);
    }
}

addResoureHandler:指的是对外暴露的访问路径
addResourceLocations:指的是内部文件放置的目录

2.6 configureDefaultServletHandling:默认静态资源处理器

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
        configurer.enable("defaultServletName");
}

此时会注册一个默认的Handler:DefaultServletHttpRequestHandler,这个Handler也是用来处理静态文件的,它会尝试映射/。当DispatcherServelt映射/时(/ 和/ 是有区别的),并且没有找到合适的Handler来处理请求时,就会交给DefaultServletHttpRequestHandler 来处理。注意:这里的静态资源是放置在web根目录下,而非WEB-INF 下。
  可能这里的描述有点不好懂(我自己也这么觉得),所以简单举个例子,例如:在webroot目录下有一个图片:1.png 我们知道Servelt规范中web根目录(webroot)下的文件可以直接访问的,但是由于DispatcherServlet配置了映射路径是:/ ,它几乎把所有的请求都拦截了,从而导致1.png 访问不到,这时注册一个DefaultServletHttpRequestHandler 就可以解决这个问题。其实可以理解为DispatcherServlet破坏了Servlet的一个特性(根目录下的文件可以直接访问),DefaultServletHttpRequestHandler是帮助回归这个特性的。

标签:var1,springboot,void,classpath,详解,registry,WebMvcConfigurer,addResourceLocations
From: https://www.cnblogs.com/lori/p/17083544.html

相关文章

  • pytest.main()参数详解
      importpytestpytest.main(["-n3"])#3个进程并发执行,需要装pytest-xdist库pytest.main(["--collect-only"])#仅仅展......
  • Java并发编程——CompletableFuture详解
    一、简介JDK5引入了Future模式。Future接口是Java多线程Future模式的实现,在java.util.concurrent包中,可以来进行异步计算。 Future模式是多线程设计常用的一种设计模式......
  • Java并发编程——ExecutorCompletionService原理详解
    一、简介在JDK并发包中有这么一个类ExecutorCompletionService,提交任务后,可以按任务返回结果的先后顺序来获取各任务执行后的结果。 该类实现了接口CompletionService:p......
  • springboot+webSocket
    1、新建WebSocketConfig配置类packagecom.config;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configurat......
  • vmstat详解
    vmstat详解 一、解读内存              Linux系统的内存分为物理内存和虚拟内存两种。物理内存是真实的,也就是物理内存条上的内存。而虚拟内......
  • mysql-mysqldump命令详解
    注意:备份加入--databases备份文件中会有创建数据库的语句,否则没有建议:导出库加入、导出表不加此参数一、mysqldump命令备份Mysql数据库的参数说明在用mysqldump备份使用......
  • mysql explain详解
    本文转载自 https://www.cnblogs.com/LoveBB/p/15181075.htmlExplain关键字是Mysql中sql优化的常用「关键字」,通常都会使用Explain来「查看sql的执行计划,而不用执行sql」......
  • PyCharm2023年安装教程:步骤详解
    PyCharm2023年安装教程:步骤详解首先,让我们介绍PyCharm,它是一款功能强大的Python集成开发环境(IDE),支持代码编写、调试、语法高亮、智能代码补全、版本控制等一系列功......
  • SpringBoot整合druid数据源
    SpringBoot默认使用HikariDataSource,但也可以整合外部DataSource。下面介绍SpringBoot_Druid整合整合有两种方式:1、starter整合 2、自定义整合1、starter整合2、自定......
  • (转)go类型转换strconv详解
    原文:https://blog.csdn.net/xixihahalelehehe/article/details/1046720691.零值变量在定义时没有明确的初始化时会赋值为零值。零值是:数值类型为0,布尔类型为false......