日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

继承 WebMvcConfigurationSupport类后无法访问Swagger页面问题

發布時間:2024/9/30 c/c++ 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 继承 WebMvcConfigurationSupport类后无法访问Swagger页面问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • 介紹WebMvcConfigurationSupport:
    • ResourceHandlerRegistry:
    • 自己項目原代碼:
    • 修改后:


項目中為了注冊攔截器寫了一個配置類繼承
WebMvcConfigurationSupport,結果居然無法訪問Swagger的接口文檔了(localhost:xxxx/doc.html),最后發現是WebMvcConfigurationSupport將靜態資源都禁止訪問了,需重寫一個資源攔截策略

介紹WebMvcConfigurationSupport:

這是提供 MVC Java 配置背后的配置的主類。 它通常通過將@EnableWebMvc添加到應用程序@Configuration類來導入。 另一種更高級的選項是直接從此類擴展并根據需要覆蓋方法
記住將@Configuration添加到子類和@Bean以覆蓋@Bean方法。 有關更多詳細信息,請參閱@EnableWebMvc的 javadoc。
此類注冊以下HandlerMappings :

RequestMappingHandlerMapping排序為 0,用于將請求映射到帶注釋的控制器方法。

HandlerMapping在 1 處排序以將 URL 路徑直接映射到視圖名稱。

BeanNameUrlHandlerMapping在 2 處排序以將 URL 路徑映射到控制器 bean 名稱。

RouterFunctionMapping在 3 RouterFunctionMapping訂購以映射路由器功能。

在WebMvcConfigurationSupport類中addResourceHandlers是一個空方法:

/*** Override this method to add resource handlers for serving static resources.* @see ResourceHandlerRegistry*/protected void addResourceHandlers(ResourceHandlerRegistry registry) {}

ResourceHandlerRegistry:

存儲資源處理程序的注冊,用于通過 Spring MVC 提供靜態資源,例如圖像、css 文件和其他資源,包括設置為在 Web 瀏覽器中高效加載而優化的緩存標頭。 可以從 Web 應用程序根目錄下的位置、類路徑和其他位置提供資源。
要創建資源處理程序,請使用addResourceHandler(String…)提供應為其調用處理程序以提供靜態資源(例如"/resources/…" )的 URL 路徑模式。
然后在返回的ResourceHandlerRegistration上使用其他方法來添加一個或多個從中提供靜態內容的位置(例如 { “/” , “classpath:/META-INF/public-web-resources/” })或指定緩存服務資源的時間。
addResourceHandler方法:添加資源處理程序以提供靜態資源。 為匹配指定 URL 路徑模式之一的請求調用處理程序。
支持諸如"/static/**“或”/css/{filename:\w+\.css}"類的模式。
源碼:

public ResourceHandlerRegistration addResourceHandler(String... pathPatterns) {ResourceHandlerRegistration registration = new ResourceHandlerRegistration(pathPatterns);this.registrations.add(registration);return registration; }

自己項目原代碼:

@Configuration // @EnableTransactionManagement public class MyConfig extends WebMvcConfigurationSupport {@Autowired private RepeatInterceptor repeatInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(repeatInterceptor);}//super.addInterceptors(registry)// .excludePathPatterns("/doc.html") ;//添加重復提交的攔截器}

修改后:

@Configuration // @EnableTransactionManagement public class MyConfig extends WebMvcConfigurationSupport {//在配置攔截器時,可以繼承WebMvcConfigurationSupport,也可以實現WebMvcConfigurer,但繼承WebMvcConfigurationSupport類是會導致自動配置失效的。////這是因為在 springboot的web自動配置類 WebMvcAutoConfiguration 上有條件注解////@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)////這個注解的意思是在項目類路徑中 缺少 WebMvcConfigurationSupport類型的bean時改自動配置類才會生效,所以繼承 WebMvcConfigurationSupport 后需要自己再重寫相應的方法。////這時候就需要重新指定靜態資源@Autowired private RepeatInterceptor repeatInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(repeatInterceptor).excludePathPatterns("/doc.html");}@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");super.addResourceHandlers(registry);}}

再次訪問項目路徑的doc.html:

總結

以上是生活随笔為你收集整理的继承 WebMvcConfigurationSupport类后无法访问Swagger页面问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。