SpringBoot關(guān)于WebMvcConfigurerAdapter

WebMvcConfigurerAdapter是Spring內(nèi)部的一種配置方式,
采用JavaBean的形式來代替?zhèn)鹘y(tǒng)的web.xml配置文件形式進(jìn)行針對框架個性化定制。

關(guān)于WebMvcConfigurerAdapter常用的方法

/** 解決跨域問題 **/
public void addCorsMappings(CorsRegistry registry) ;

/** 添加攔截器 **/
void addInterceptors(InterceptorRegistry registry);

/** 這里配置視圖解析器 **/
void configureViewResolvers(ViewResolverRegistry registry);

/** 配置內(nèi)容裁決的一些選項(xiàng) **/
void configureContentNegotiation(ContentNegotiationConfigurer configurer);

/** 視圖跳轉(zhuǎn)控制器 **/
void addViewControllers(ViewControllerRegistry registry);

/** 靜態(tài)資源處理 **/
void addResourceHandlers(ResourceHandlerRegistry registry);

/** 默認(rèn)靜態(tài)資源處理器 **/
void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer);

1. 攔截器

//registry.addInterceptor:需要一個實(shí)現(xiàn)HandlerInterceptor接口的攔截器實(shí)例
//registry.addPathPatterns:用于設(shè)置攔截器的過濾路徑規(guī)則
//registry.excludePathPatterns:用于設(shè)置不需要攔截的過濾規(guī)則
@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:跳轉(zhuǎn)指定頁面

@Override
 public void addViewControllers(ViewControllerRegistry registry) {
     super.addViewControllers(registry);
     registry.addViewController("/").setViewName("/index");
     //實(shí)現(xiàn)一個請求到視圖的映射,而無需書寫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:信息轉(zhuǎn)換器

/**
* 消息內(nèi)容轉(zhuǎn)換配置
 * 配置fastJson返回json轉(zhuǎn)換
 * @param converters
 */
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    //調(diào)用父類的配置
    super.configureMessageConverters(converters);
    //創(chuàng)建fastJson消息轉(zhuǎn)換器
    FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
    //創(chuàng)建配置類
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    //修改配置返回內(nèi)容的過濾
    fastJsonConfig.setSerializerFeatures(
            SerializerFeature.DisableCircularReferenceDetect,
            SerializerFeature.WriteMapNullValue,
            SerializerFeature.WriteNullStringAsEmpty
    );
    fastConverter.setFastJsonConfig(fastJsonConfig);
    //將fastjson添加到視圖消息轉(zhuǎn)換器列表內(nèi)
    converters.add(fastConverter);

}

6、addResourceHandlers:靜態(tài)資源

@Override
 public void addResourceHandlers(ResourceHandlerRegistry registry) {
    //處理靜態(tài)資源的,例如:圖片,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."。

可以通過實(shí)現(xiàn)WebMvcConfigurer或者繼承WebMvcConfigurationSupport。
但有一點(diǎn)需要注意,如果你是升級舊有的應(yīng)用程序,需要將方法中對super()的調(diào)用代碼清除。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容