WebMvcConfigurerAdapter详解和过时后的替代方案
一、什么是WebMvcConfigurerAdapter
Spring内部的一种配置方式
采用JavaBean的形式来代替传统的xml配置文件形式进行针对框架个性化定制
二、WebMvcConfigurerAdapter常用的方法
/** 解决跨域问题 **/
public void addCorsMappings(CorsRegistry registry) ; /** 添加拦截器 **/
void addInterceptors(InterceptorRegistry registry); /** 这里配置视图解析器 **/
void configureViewResolvers(ViewResolverRegistry registry); /** 配置内容裁决的一些选项 **/
void configureContentNegotiation(ContentNegotiationConfigurer configurer); /** 视图跳转控制器 **/
void addViewControllers(ViewControllerRegistry registry); /** 静态资源处理 **/
void addResourceHandlers(ResourceHandlerRegistry registry); /** 默认静态资源处理器 **/
void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer);
1、addInterceptors:拦截器
- addInterceptor:需要一个实现HandlerInterceptor接口的拦截器实例
- addPathPatterns:用于设置拦截器的过滤路径规则
excludePathPatterns:用于设置不需要拦截的过滤规则
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**").excludePathPatterns("/toLogin","/login");
super.addInterceptors(registry);
}
2、addCorsMappings:跨域
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("*").allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE").maxAge(3600).allowCredentials(true);
}
3、addViewControllers:跳转指定页面
/**
* 以前要访问一个页面需要先创建个Controller控制类,在写方法跳转到页面
* 在这里配置后就不需要那么麻烦了,直接访问http://localhost:8080/toLogin就跳转到login.html页面了
*
* @param registry
*/
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/toLogin").setViewName("login");
registry.addViewController("/").setViewName("/index");
registry.addViewController("/login").setViewName("forward:/index.html");
super.addViewControllers(registry);
}
4、resourceViewResolver:视图解析器
/**
* 配置请求视图映射
* @return
*/
@Bean
public InternalResourceViewResolver resourceViewResolver()
{
InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
//请求视图文件的前缀地址
internalResourceViewResolver.setPrefix("/WEB-INF/jsp/");
//请求视图文件的后缀
internalResourceViewResolver.setSuffix(".jsp");
return internalResourceViewResolver;
} /**
* 视图配置
* @param registry
*/
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
super.configureViewResolvers(registry);
registry.viewResolver(resourceViewResolver());
/*registry.jsp("/WEB-INF/jsp/",".jsp");*/
}
5、configureMessageConverters:信息转换器
/**
* 消息内容转换配置
* 配置fastJson返回json转换
* @param converters
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//调用父类的配置
super.configureMessageConverters(converters);
//创建fastJson消息转换器
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//创建配置类
FastJsonConfig fastJsonConfig = new FastJsonConfig();
//修改配置返回内容的过滤
fastJsonConfig.setSerializerFeatures(
SerializerFeature.DisableCircularReferenceDetect,
SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullStringAsEmpty
);
fastConverter.setFastJsonConfig(fastJsonConfig);
//将fastjson添加到视图消息转换器列表内
converters.add(fastConverter); }
6、addResourceHandlers:静态资源
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//自定义项目内目录
//registry.addResourceHandler("/my/**").addResourceLocations("classpath:/my/");
//指向外部目录
registry.addResourceHandler("/my/**").addResourceLocations("file:E:/my/");
super.addResourceHandlers(registry);
}
三、使用WebMvcConfigurerAdapter
1、过时方式:继承WebMvcConfigurerAdapter
该方法在spring boot 2.0,Spring 5.0 之后,已经被废弃
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
//TODO
}
2、代替方案
①直接实现WebMvcConfigurer
@Configuration
public class WebMvcConfg implements WebMvcConfigurer {
//TODO
}
②直接继承WebMvcConfigurationSupport
@Configuration
public class WebMvcConfg extends WebMvcConfigurationSupport {
//TODO
}
查看源码发现: WebMvcConfigurerAdapter只是对WebMvcCofigurer的空实现
WebMvcConfigurationSupport与WebMvcConfigurerAdapter、接口WebMvcConfigurer处于同一个目录下
WebMvcConfigurationSupport包含WebMvcConfigurer里面的方法,且WebMvcConfigurationSupport的实现的方法更全面
但是继承WebMvcConfigurationSupport会发现Spring Boot的WebMvc自动配置失效(WebMvcAutoConfiguration自动化配置),导致无法视图解析器无法解析并返回到对应的视图。
关于使用新方案会出现的问题,可以参考下面这篇文章。
WebMvcConfigurer 与 WebMvcConfigurationSupport避坑指南
WebMvcConfigurerAdapter详解和过时后的替代方案的更多相关文章
- Hadoop3.1.1源码Client详解 : Packet入队后消息系统运作之DataStreamer(Packet发送) : 主干
该系列总览: Hadoop3.1.1架构体系——设计原理阐述与Client源码图文详解 : 总览 在上一章(Hadoop3.1.1源码Client详解 : 写入准备-RPC调用与流的建立) 我们提到, ...
- Hadoop3.1.1源码Client详解 : Packet入队后消息系统运作之ResponseProcessor(ACK接收)
该系列总览: Hadoop3.1.1架构体系——设计原理阐述与Client源码图文详解 : 总览 紧接着上一篇文章: Hadoop3.1.1源码Client详解 : Packet入队后消息系统运作之D ...
- SpringBoot——》WebMvcConfigurerAdapter详解
一.WebMvcConfigurerAdapter是什么二.WebMvcConfigurerAdapter常用的方法1.addInterceptors:拦截器2.addCorsMappings:跨域3 ...
- Java注释Override、Deprecated、SuppressWarnings详解(过时方法,即将删除的方法或成员变量)
Override 这个注释的作用是标识某一个方法是否覆盖了它的父类的方法.那么为什么要标识呢?让我们来看看如果不用Override标识会发生什么事情. Deprecated 这个注释是一个标记注释.所 ...
- Spring 梳理 - WebMvcConfigurerAdapter详解
参考:https://blog.csdn.net/weixin_43453386/article/details/83623242
- Influxdb原理详解
本文属于<InfluxDB系列教程>文章系列,该系列共包括以下 15 部分: InfluxDB学习之InfluxDB的安装和简介 InfluxDB学习之InfluxDB的基本概念 Infl ...
- 26.C++- 泛型编程之类模板(详解)
在上章25.C++- 泛型编程之函数模板(详解) 学习了后,本章继续来学习类模板 类模板介绍 和函数模板一样,将泛型思想应用于类. 编译器对类模板处理方式和函数模板相同,都是进行2次编译 类模板通 ...
- flask基础之session原理详解(十)
前言 flask_session是flask框架实现session功能的一个插件,用来替代flask自带的session实现机制,flask默认的session信息保存在cookie中,不够安全和灵活 ...
- Hadoop3.1.1架构体系——设计原理阐述与Client源码图文详解 : 总览
一.设计原理 1.Hadoop架构: 流水线(PipeLine) 2.Hadoop架构: HDFS中数据块的状态及其切换过程,GS与BGS 3.Hadoop架构: 关于Recovery (Lease ...
随机推荐
- Linux--操作系统基础及基础命令--01
一.系统基础 1.三大部件: CPU:运算器.控制器.存储器 内存:CPU的数据只能从内存中读取,且内存数据是易失性的(页面) IO: 控制总线.数据总线 2.OS的管理 GUI:图形用户界面 GNO ...
- zencart批量表上传后 标题显示为网址 批量修改标题状态 SEO三要素
zencart批量表上传后 标题显示为网址,原因是导入批量表时,产品标题对应状态被重置为0导致的 批量修改标题状态 ', metatags_products_name_status ', metata ...
- 2017 ICPC 南宁 L 带权最大递增子序列
#include<cstdio> #include<iostream> #include<cstring> #include<cmath> #inclu ...
- Vim搜索关键字
有以下两种方法 Method 1:/content 默认从上往下查找 只读模式下输入 /content 后回车 按 n 向下查找 按N 向上查找 Method 2:?content 默认从下往上查找 ...
- 初识Python,利用turtle画图
目录 我的第三篇博客 一.初识Python 1.变量 2.注释 3.turtle库 我的第三篇博客 一.初识Python 1.变量 变量就是可变的的量,用来描述某个事物的属性.本质作用就是描述和接收变 ...
- 【NOIP2016提高A组模拟8.14】总结
第一题是几何题,没去想直接弃疗.... 第二题觉得很像背包,但是单挑人的顺序不同,答案也会不同,我比较了每个人先后的优劣性,成功搞定了这道题.但是再输出时不小心搞错了,爆零. 第三题,我答案了整整一个 ...
- xgboost使用细节
from http://blog.csdn.net/zc02051126/article/details/46771793 在Python中使用XGBoost 下面将介绍XGBoost的Python模 ...
- Sublime text3配置C/C++编译环境
安装sublime text3后,一直很喜欢使用它看代码(这个高亮配色真的很好看).它默认的运行环境就有C/C++,在写了一个hello world!后正常输出,但在加入scanf()输入后就不行了. ...
- mysql-5.6.45-linux-glibc2.12-x86_64.tar.gz下载安装
一 ,mysql下载 需要注册,可以通过组合url越过注册下载需要的包. 下载地址: https://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.3 ...
- BZOJ 1022 Luogu P4279 [SHOI2008]小约翰的游戏 (博弈论)
题目链接: (bzoj) https://www.lydsy.com/JudgeOnline/problem.php?id=1022 (luogu) https://www.luogu.org/pro ...