一、WebMvcConfigurerAdapter是什么
二、WebMvcConfigurerAdapter常用的方法
1、addInterceptors:拦截器
2、addCorsMappings:跨域
3、addViewControllers:跳转指定页面
4、resourceViewResolver:视图解析器
5、configureMessageConverters:信息转换器
6、addResourceHandlers:静态资源
三、WebMvcConfigurerAdapter使用方式
1、过时方式:继承WebMvcConfigurerAdapter
2、现用方式
1)实现WebMvcConfigurer
2)现用方式:继承WebMvcConfigurationSupport
一、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) {
super.addInterceptors(registry);
registry.addInterceptor(new TestInterceptor()).addPathPatterns("/**");
}

2、addCorsMappings:跨域
@Override
public void addCorsMappings(CorsRegistry registry) {
super.addCorsMappings(registry);
registry.addMapping("/cors/**")
.allowedHeaders("*")
.allowedMethods("POST","GET")
.allowedOrigins("*");
}

3、addViewControllers:跳转指定页面
@Override
public void addViewControllers(ViewControllerRegistry registry) {
super.addViewControllers(registry);
registry.addViewController("/").setViewName("/index");
//实现一个请求到视图的映射,而无需书写controller
registry.addViewController("/login").setViewName("forward:/index.html");
}

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) {
//处理静态资源的,例如:图片,js,css等
registry.addResourceHandler("/resource/**").addResourceLocations("/WEB-INF/static/");
}

三、WebMvcConfigurerAdapter使用方式
1、过时方式:继承WebMvcConfigurerAdapter
Spring 5.0 以后WebMvcConfigurerAdapter会取消掉
WebMvcConfigurerAdapter是实现WebMvcConfigurer接口

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
//TODO
}

2、现用方式
1)实现WebMvcConfigurer
@Configuration
public class WebMvcConfg implements WebMvcConfigurer {
//TODO
}

2)现用方式:继承WebMvcConfigurationSupport
@Configuration
public class WebMvcConfg extends WebMvcConfigurationSupport {
//TODO
}
————————————————
版权声明:本文为CSDN博主「小仙。」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_43453386/article/details/83623242

SpringBoot——》WebMvcConfigurerAdapter详解的更多相关文章

  1. springboot配置详解

    springboot配置详解 Author:SimpleWu properteis文件属性参考大全 springboot默认加载配置 SpringBoot使用两种全局的配置文件,全局配置文件可以对一些 ...

  2. springboot项目--传入参数校验-----SpringBoot开发详解(五)--Controller接收参数以及参数校验----https://blog.csdn.net/qq_31001665/article/details/71075743

    https://blog.csdn.net/qq_31001665/article/details/71075743 springboot项目--传入参数校验-----SpringBoot开发详解(五 ...

  3. SpringBoot @ConfigurationProperties详解

    文章目录 简介 添加依赖关系 一个简单的例子 属性嵌套 @ConfigurationProperties和@Bean 属性验证 属性转换 自定义Converter SpringBoot @Config ...

  4. Spring Boot2 系列教程 (二) | 第一个 SpringBoot 工程详解

    微信公众号:一个优秀的废人 如有问题或建议,请后台留言,我会尽力解决你的问题. 前言 哎呦喂,按照以往的惯例今天周六我的安排应该是待在家学学猫叫啥的.但是今年这种日子就可能一去不复返了,没法办法啊.前 ...

  5. 精通SpringBoot:详解WebMvcConfigurer接口

    SpringBoot 确实为我们做了很多事情, 但有时候我们想要自己定义一些Handler,Interceptor,ViewResolver,MessageConverter,该怎么做呢.在Sprin ...

  6. Springboot 启动详解

    1.前言 最近一直在看Springboot和springcloud代码,看了将近20多天,对这两个系统的认知总算是入了门.后续应该会有一个系列的文章,本文就先从Springboot的启动入手. 2.容 ...

  7. 2.SpringBoot HelloWorld详解

    1.POM文件 父项目 <parent> <groupId>org.springframework.boot</groupId> <artifactId> ...

  8. swagger搭建(基于springBoot)详解

    前后端分离后,api接口文档的维护就成了一个让人头疼的问题,api接口更新慢,或因开发工作量大,没时间整理文档,导致前后端分离后前端同学和后端同 学都纠结于文档的问题.而swagger的出现,不亚于一 ...

  9. SpringBoot 配置文件详解

    springboot采纳了建立生产就绪spring应用程序的观点. Spring Boot优先于配置的惯例,旨在让您尽快启动和运行.在一般情况下,我们不需要做太多的配置就能够让spring boot正 ...

随机推荐

  1. Vue代码分割懒加载的实现方法

    什么是懒加载 懒加载也叫延迟加载,即在需要的时候进行加载,随用随载. 为什么需要懒加载 在单页应用中,如果没有应用懒加载,运用webpack打包后的文件将会异常的大,造成进入首页时,需要加载的内容过多 ...

  2. oracle——学习之路(select检索)

    select语法: select   [distinct|all]    列名     from   表名     [where]   [group by]   [having]    [order ...

  3. python — 函数基础知识(一)

    目录 1 面向过程编程与函数式编程 2 函数的基本结构 3 函数的参数 1 面向过程编程与函数式编程 截至目前我们所接触.所写的编程为:面向过程式编程[可读性差/可重用性差] # 面向过程编程 use ...

  4. 牛客 26C 手铐 (缩环, 树形dp)

    先缩环建树, 对于树上个环$x,y$, 假设$x,y$路径上有$cnt$个环(不包括$x,y$), 贡献就为$2^{cnt}$. 这题卡常挺严重的, 刚开始用并查集合并竟然T了. #include & ...

  5. 研究旧项目, 常用 sql 语句

    1. select all table select TABLE_NAME from CodingSystem.INFORMATION_SCHEMA.TABLES where TABLE_TYPE = ...

  6. Centos 7.3 搭建php7,mysql5.7,nginx1.10.1,redis

    一.安装nginx 更新系统软件(非必要) # yum update 安装nginx 1.下载nginx # wget http://nginx.org/download/nginx-1.15.2.t ...

  7. 怎样禁用浏览器的Cookie功能

    使用: window.navigator.cookieEnabled; window.navigator.cookieEnabled = true; 这样设置以后, 浏览器就不会接受和保存服务器传过来 ...

  8. Java秒杀实战 (六) 服务级高并发秒杀优化(RabbitMQ+接口优化)

    转自:https://blog.csdn.net/qq_41305266/article/details/81146716 一.思路:减少数据库访问 1.系统初始化,把商品库存数量加载到Redis 2 ...

  9. linux下Django Nginx+uwsgi 安装配置

    原文链接 在前面的章节中我们使用 python manage.py runserver 来运行服务器.这只适用测试环境中使用. 正式发布的服务,我们需要一个可以稳定而持续的服务器,比如apache, ...

  10. RobHess的SIFT代码解析之kd树

    平台:win10 x64 +VS 2015专业版 +opencv-2.4.11 + gtk_-bundle_2.24.10_win32 主要参考:1.代码:RobHess的SIFT源码:SIFT+KD ...