在spring-boot-2.2.5中对MVC自动配置类进行的更改,之前的WebMvcConfigurerAdapter类声明为过时的,现在进行自定义扩展需要实现WebMvcConfigurer类重写其中的方法进行扩展

官方解释

If you want to keep those Spring Boot MVC customizations and make more MVC customizations (interceptors, formatters, view controllers, and other features), you can add your own @Configuration class of type WebMvcConfigurer but without @EnableWebMvc.
如果要保留这些Spring Boot MVC定制并进行更多的MVC定制(拦截器,格式化程序,视图控制器和其他功能),则可以添加自己的类型为WebMvcConfigurer的@Configuration类,但不添加 @EnableWebMvc。
注意:
1、扩展功能是添加配置类去实现 WebMvcconfigurer 类,springboot的自动默认配置同事生效
2、如果实现的是:WebMvcConfigurationSupport类,则springboot默认的自动配置类不生效
在总动配置类上有@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)注解
原理:

@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class,ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {

创建MyMvcConfig类

addViewControllers:页面跳转
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {@Overridepublic void addViewControllers(ViewControllerRegistry registry) {registry.addViewController("/success").setViewName("success");}
}

启动项目访问

addInterceptors:拦截器

1) addInterceptor:需要一个实现HandlerInterceptor接口的拦截器实例或者WebRequestInterceptor接口的实例

2)在实现了webMvcConfigur类的自定义配置类中重写addInterceptor方法,将自定义的拦截器进行注册

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

先创建自定义的拦截器类MyIntercepter
自定义的interceptinterceptor可以实现HandlerInterceptor或者WebRequestInterceptor
然后将自定义的拦截器进行注册

public class MyMvcInterceptor implements HandlerInterceptor {@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {System.out.println("afterCompletion");}/*** 这里是有一个返回值的,如果返回值为true,继续执行,如果返回值为false则不执行后续的操作* 这个方法在WebRequestInterceptor中则无返回值*/@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {//ServletRequestUtils.getStringParameter(request,"ss");System.out.println("preHandle");return true;}@Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {System.out.println("postHandle");}
}
public class MyMvcRequestInterceptor implements WebRequestInterceptor {@Overridepublic void preHandle(WebRequest request) throws Exception {System.out.println("r----------preHandle");}@Overridepublic void postHandle(WebRequest request, ModelMap model) throws Exception {System.out.println("r----------postHandle");}@Overridepublic void afterCompletion(WebRequest request, Exception ex) throws Exception {System.out.println("r----------afterCompletion");}
}

在配置类中配置自定义拦截器及规则,使拦截器生效

 @Overridepublic void addInterceptors(InterceptorRegistry registry) {InterceptorRegistration interceptorRegistration = registry.addInterceptor(new MyInterceper()).addPathPatterns("/**").excludePathPatterns("/hello");}
addResourceHandlers:静态资源

springboot中默认的静态资源映射规则是通过url/webjars/** 去请求classpath:/WATE-INF/resources/webjars/下的内容.

WebMvcAutoConfiguration.java:中的默认规则如下:

@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {if (!this.resourceProperties.isAddMappings()) {logger.debug("Default resource handling disabled");return;}Duration cachePeriod = this.resourceProperties.getCache().getPeriod();CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();if (!registry.hasMappingForPattern("/webjars/**")) {customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/").setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));}String staticPathPattern = this.mvcProperties.getStaticPathPattern();if (!registry.hasMappingForPattern(staticPathPattern)) {customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern).addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));}}

如果我们需要自定义静态资源映射目录的话,只需重写addResourceHandlers方法即可。
addResoureHandler:指的是对外暴露的访问路径
addResourceLocations:指的是内部文件放置的目录

 @Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("/mySource/**").addResourceLocations("/my/");}
configureDefaultServletHandling:默认静态资源处理器

通过转发到Servlet容器的“默认” Servlet,配置一个处理程序以委派未处理的请求。常见的用法是将映射到“ /”,从而覆盖Servlet容器对静态资源的默认处理。。

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {configurer.enable();configurer.enable("defaultServletName");
configureViewResolvers:视图解析器

重写此方法可以扩展视图解析器

   @Overridepublic void configureViewResolvers(ViewResolverRegistry registry) {InternalResourceViewResolver jspViewResolver = new InternalResourceViewResolver();jspViewResolver.setPrefix("/myJsp/");jspViewResolver.setSuffix(".jsp");jspViewResolver.setViewNames("*");registry.viewResolver(jspViewResolver);}

springboot-2.2.5中自定义拦截器、静态资源映射、视图控制器和其他功能相关推荐

  1. SpringBoot中自定义拦截器

    场景 自定义拦截器,通过继承WebMvcConfigureAdapter然后重写父类中的方法进行扩展. 项目搭建专栏: https://blog.csdn.net/BADAO_LIUMANG_QIZH ...

  2. Spring mvc中自定义拦截器

    一.要实现的一个功能: 1.打开特定的一些页面时必需强制用户进行登录. 2.登录后再返回到之前的页面. 二.先写一个service,实现从cookie中判断用户是否登录. 1.TT_TOKEN为登录成 ...

  3. 使用 imitator 实现前后端分离开发中的数据模拟与静态资源映射

    imitator 一个简单易用的 nodejs 服务器, 主要用于模拟 HTTP 接口数据, 请求代理与转发 . 使用imitator,可以解决前后端分离开发中的痛点之一:数据模拟,也可以作为代理服务 ...

  4. JavaEE开发之SpringMVC中的自定义拦截器及异常处理

    上篇博客我们聊了<JavaEE开发之SpringMVC中的路由配置及参数传递详解>,本篇博客我们就聊一下自定义拦截器的实现.以及使用ModelAndView对象将Controller的值加 ...

  5. EasyExcel——采用自定义拦截器设置单元格列宽

    文章目录 前言 EasyExcel 版本 自定义拦截器 使用 前言 在EasyExcel的官方文档中,有一个自定义拦截器的配置与使用讲解. 自定义拦截器(上面几点都不符合但是要对单元格进行操作的参照这 ...

  6. 在SpringBoot项目中整合拦截器

    拦截器在Web系统中非常常见,对于某些全局统一的操作,我们可以把它提取到拦截器中实现.总结起来,拦截器大致有以下几种使用场景: 1.权限检查:如登录检测,进入处理程序检测用户是否登录,如果没有,则直接 ...

  7. WebServices中使用cxf开发日志拦截器以及自定义拦截器

    首先下载一个cxf实例,里面包含cxf的jar包.我下的是apache-cxf-2.5.9 1.为什么要设置拦截器? 为了在webservice请求过程中,能动态操作请求和响应数据, CXF设计了拦截 ...

  8. 在struts2中配置自定义拦截器放行多个方法

    源码: 自定义的拦截器类: //自定义拦截器类:LoginInterceptor ; package com.java.action.interceptor; import javax.servlet ...

  9. 使用struts2中默认的拦截器以及自定义拦截器

    转自:http://blog.sina.com.cn/s/blog_82f01d350101echs.html 如何使用struts2拦截器,或者自定义拦截器.特别注意,在使用拦截器的时候,在Acti ...

最新文章

  1. [转载] 晓说——第3期:梦回青楼 爱与自由的温柔乡(上)
  2. 2个多边形,其中一个包围另一个,如何将中间的环带区域涂成红色
  3. Windows Phone 7 Bitmap编码
  4. 总算解决了路由器上iptables的nat问题
  5. python模型部署方法_终极开箱即用的自动化Python模型选择方法
  6. php充值注入,PHP注入一路小跑
  7. docker mysql配置 丢失_Ubuntu16.04服务器环境配置 – Docker、MySQL、Redis
  8. vue 时间戳 格式转化(插件化) - 封装篇
  9. 【python教程入门学习】学python要多久,0基础学python有多难
  10. 实验八 java多线程操作_20182310实验八实验报告
  11. java java 检查型异常_如何整合Java中的有效性检查和异常抛出?
  12. maven详解scope
  13. android 控件突然变小,android中自定义控件
  14. Linux下vsftpd的安装,Java上传文件实现。
  15. [转载]Oracle Minus关键字
  16. 源支付源码客户端+云端+监控+协议三网免挂免输入(全套版)
  17. html5游戏毕业答辩ppt,毕业论文答辩ppt格式(超详细解释)
  18. Android Studio实现用户登陆界面demo(xml实现)
  19. 皮克定理(格点三角形求面积或求三角形里格点(整点)个数)
  20. 一份自己整理的不太详细的常见面试题

热门文章

  1. Android瘦身之tiny图片处理
  2. Centos 安装docker后 deamo 无法启动的问题 解决
  3. 炸金花游戏(3)--基于EV(期望收益)的简单AI模型
  4. 【机试题】2014大疆嵌入式笔试题(附超详细解答,下篇)
  5. ROS用python编写订阅者和发布者(使用存放在其他package的自定义msg文件)
  6. IE不能下载MSG文件的解决方案
  7. 分布式事务解决方案(总览)
  8. e代驾——打造代驾服务标准化平台
  9. 发现网站被劫持该怎么办?网站域名劫持的情况及解决办法
  10. 美多商城之商品(2)