WebMvcConfigurer、WebMvcConfigurationSupport、
1 简介
WebMvcConfigurerAdapter、WebMvcConfigurer、WebMvcConfigurationSupport、WebMvcAutoConfiguration
首先以上4个类都是springboot中mvc的支持类
2 使用
2.1 WebMvcConfigurer
WebMvcConfigurer 为接口
2.2 WebMvcConfigurerAdapter
WebMvcConfigurerAdapter 是 WebMvcConfigurer 的实现类大部分为空方法 ;是WebMvcConfigurer的子类实现,由于Java8中可以使用default关键字为接口添加默认方法,为在源代码中spring5.0之后就已经弃用本类。如果需要我接着可以实现WebMvcConfigurer类
2.3 WebMvcConfigurationSupport
WebMvcConfigurationSupport 是mvc的基本实现并包含了WebMvcConfigurer接口中的方法
2.4 WebMvcAutoConfiguration
WebMvcAutoConfiguration 是mvc的自动装在类并部分包含了WebMvcConfigurer接口中的方法,继承WebMvcConfigurationSupport或者@EnableWebMvc 会导致WebMvcAutoConfiguration不加载,所以默认配置丢失导致静态资源等无法访问; 需要@Override addResourceHandlers
如果在springboot项目中没有使用到以上类,那么会自动启用WebMvcAutoConfiguration类做自动加载; 项目中的配置都是默认的,比如静态资源文件的访问
3 拦截器
添加自己的拦截器addInterceptors:
方案一:
继承WebMvcConfigurationSupport 会导致WebMvcAutoConfiguration不加载,所以默认配置丢失导致静态资源等无法访问; 需要@Override addResourceHandlers
方案二
实现继承WebMvcConfigurer因为是接口实现所以WebMvcAutoConfiguration还会默认配置,所以静态资源是可以访问的
4 方法
4.1 addInterceptors:拦截器
/** 添加拦截器 **/
void addInterceptors(InterceptorRegistry registry);
addInterceptor:需要一个实现HandlerInterceptor接口的拦截器实例
addPathPatterns:用于设置拦截器的过滤路径规则
excludePathPatterns:用于设置不需要拦截的过滤规则
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**").excludePathPatterns("/toLogin","/login");
super.addInterceptors(registry);
}
4.2 addCorsMappings:跨域
/** 解决跨域问题 **/
public void addCorsMappings(CorsRegistry registry) ;
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("*").allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE").maxAge(3600).allowCredentials(true);
}
4.3 addViewControllers:跳转指定页面
/** 视图跳转控制器 **/
void addViewControllers(ViewControllerRegistry registry);
/**
* 以前要访问一个页面需要先创建个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.4 视图配置
/** 这里配置视图解析器 **/
void configureViewResolvers(ViewResolverRegistry registry);
/**
- 视图配置
- @param registry
*/
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
super.configureViewResolvers(registry);
registry.viewResolver(resourceViewResolver());
/registry.jsp(“/WEB-INF/jsp/”,“.jsp”);/
}
/**
- 配置请求视图映射
- @return
*/
@Bean
public InternalResourceViewResolver resourceViewResolver()
{
InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
//请求视图文件的前缀地址
internalResourceViewResolver.setPrefix(“/WEB-INF/jsp/”);
//请求视图文件的后缀
internalResourceViewResolver.setSuffix(“.jsp”);
return internalResourceViewResolver;
}
4.5 addResourceHandlers:静态资源
/** 静态资源处理 **/
void addResourceHandlers(ResourceHandlerRegistry registry);
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//自定义项目内目录
//registry.addResourceHandler("/my/**").addResourceLocations("classpath:/my/");
//指向外部目录
registry.addResourceHandler("/my/**").addResourceLocations("file:E:/my/");
super.addResourceHandlers(registry);
}
configureMessageConverters:信息转换器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);
}
/** 配置内容裁决的一些选项 **/
void configureContentNegotiation(ContentNegotiationConfigurer configurer);
/** 默认静态资源处理器 **/
void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer);