美文网首页
SpringBoot关于WebMvcConfigurerAdap

SpringBoot关于WebMvcConfigurerAdap

作者: lew1sss | 来源:发表于2019-07-23 15:15 被阅读0次

WebMvcConfigurerAdapter是Spring内部的一种配置方式,
采用JavaBean的形式来代替传统的web.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. 拦截器

//registry.addInterceptor:需要一个实现HandlerInterceptor接口的拦截器实例
//registry.addPathPatterns:用于设置拦截器的过滤路径规则
//registry.excludePathPatterns:用于设置不需要拦截的过滤规则
@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(new TestInterceptor()).addPathPatterns("/**");
}

2、addCorsMappings:跨域

@Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("*")
                .allowedHeaders("*")
                .exposedHeaders("Access-Control-Allow-Origin", "Access-Control-Allow-Credentials")
                .allowCredentials(false).maxAge(3600);
    }

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使用方法

在spring4.0或者springboot1.5版本下,通过继承WebMvcConfigurerAdapter,即可。

但是要使用spring5.0或者springboot2.0以上,使用上面的方法,会有警告"Warning:The type WebMvcConfigurerAdapter is deprecated."

可以通过实现WebMvcConfigurer或者继承WebMvcConfigurationSupport。
但有一点需要注意,如果你是升级旧有的应用程序,需要将方法中对super()的调用代码清除。

相关文章

网友评论

      本文标题:SpringBoot关于WebMvcConfigurerAdap

      本文链接:https://www.haomeiwen.com/subject/yyaolctx.html