溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

spring boot攔截器WebMvcConfigurerAdapter,以及高版本的替換方案

發(fā)布時(shí)間:2020-06-24 11:08:19 來源:網(wǎng)絡(luò) 閱讀:108544 作者:it林工 欄目:軟件技術(shù)

最近項(xiàng)目采用spring icloud,用的spring boot版本是1.5.x的,spring boot 2.0,Spring 5.0 以后WebMvcConfigurerAdapter會(huì)取消掉。以下介紹下大體的內(nèi)容,希望對(duì)大家都有所幫助。

  • 以下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 直接實(shí)現(xiàn)WebMvcConfigurer

        @Configuration
    public class WebMvcConfg implements WebMvcConfigurer {
    
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                    registry.addViewController("/index").setViewName("index");
            }
    
    }

    方案2 直接繼承WebMvcConfigurationSupport

        @Configuration
    public class WebMvcConfg extends WebMvcConfigurationSupport {
    
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                    registry.addViewController("/index").setViewName("index");
            }
    
    }

其實(shí),源碼下WebMvcConfigurerAdapter是實(shí)現(xiàn)WebMvcConfigurer接口,所以直接實(shí)現(xiàn)WebMvcConfigurer接口也可以;WebMvcConfigurationSupport與WebMvcConfigurerAdapter、接口WebMvcConfigurer處于同一個(gè)目錄下WebMvcConfigurationSupport包含WebMvcConfigurer里面的方法,由此看來版本中應(yīng)該是推薦使用WebMvcConfigurationSupport類的,WebMvcConfigurationSupport應(yīng)該是新版本中對(duì)WebMvcConfigurerAdapter的替換和擴(kuò)展【個(gè)人見解,如果有誤,請(qǐng)幫忙糾正】

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI