WebMvcConfigurerAdapter 和 WebMvcConfigurer

一、简介

两者都是Spring内部的一种配置方式,WebMvcConfigurerAdapter 是一个类,已过期;WebMvcConfigurer是一个接口,替代方案;
采用JavaBean的形式来代替传统的xml配置文件形式进行针对框架个性化定制。

二、WebMvcConfigurerAdapter常用的方法

/**添加Converters和Formatters*/
default void addFormatters(FormatterRegistry registry)

/**拦截器*/
default void addInterceptors(InterceptorRegistry registry) 
    
/**静态资源处理器*/
default void addResourceHandlers(ResourceHandlerRegistry registry) 
    
/**解决跨域问题*/
default void addCorsMappings(CorsRegistry registry) 
    
/** 使用addViewController实现实现无业务逻辑跳转*/
default void addViewControllers(ViewControllerRegistry registry)

/**视图解析器*/
default void configureViewResolvers(ViewResolverRegistry registry)

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) {
    registry.addMapping("/cors/**")
            .allowedHeaders("*")
            .allowedMethods("POST","GET")
            .allowedOrigins("*");
}

3、addViewControllers:跳转指定页面

@Override
 public void addViewControllers(ViewControllerRegistry 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/");
 }

 

posted @ 2021-10-03 17:01  木有呂朋友  阅读(864)  评论(0编辑  收藏  举报