springboot整合swagger3報Unable to infer base url錯誤問題
springboot整swagger3
工程中的pom文件加入依賴包
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency>
代碼中配置Swagger3Config
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.oas.annotations.EnableOpenApi; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; /** * 說明:Swagger 接口API生成 * 作者:wanghan */ @Configuration @EnableOpenApi public class Swagger3Config { @Bean public Docket createRestApi() { return new Docket(DocumentationType.OAS_30) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.wanghan.ctrl")) // 為當前包路徑 .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Swagger3 RESTful API") // 頁面標題 .version("3.0") // 版本號 .description("接口文檔") // 描述 .build(); } }
Swagger 攔截配置
import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** * 說明:Swagger 攔截配置 * 作者:wanghan */ @Configuration public class WebMvcConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry. addResourceHandler("/swagger-ui/**") .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/") .resourceChain(false); } @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/swagger-ui/") .setViewName("forward:/swagger-ui/index.html"); } }
至此swagger已經(jīng)配置到你的項目中了。
可以通過url訪問了:
http://localhost:8080/swagger-ui/index.html
總是不那么順利
然而在使用過程中,總是出現(xiàn)這個那個問題:
問題一:提示沒有權限訪問
如果你使用安全框架,Swagger3的內(nèi)置接口就會訪問受限,我們需要排除掉。
Spring Security是這么配置的:
@Override public void configure(WebSecurity web) throws Exception { //忽略swagger3所需要用到的靜態(tài)資源,允許訪問 web.ignoring().antMatchers( "/swagger-ui.html", "/swagger-ui/**", "/swagger-resources/**", "/v2/api-docs", "/v3/api-docs", "/webjars/**"); }
或者你使用的版本是Spring Security 5.4,你可以這么定制??WebSecurity
@Bean WebSecurityCustomizer swaggerWebSecurityCustomizer() { return (web) -> { web.ignoring().antMatchers(new String[]{"/swagger-ui.html", "/swagger-ui/**", "/swagger-resources/**", "/v2/api-docs", "/v3/api-docs", "/webjars/**"}); }; }
問題二:報??Unable to infer base url
你會發(fā)現(xiàn)Swagger3會報??Unable to infer base url的錯誤,這是因為統(tǒng)一返回體影響到了Swagger3的一些內(nèi)置接口。
解決方法是??
@RestControllerAdvice???控制好生效的包范圍,在你的實現(xiàn)類上加上basePackages = “項目包路徑”
@ControllerAdvice(basePackages = "com.wanghan") public class ApiResBodyAdvice implements ResponseBodyAdvice { }
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java 數(shù)據(jù)結構線性表之順序存儲詳解原理
線性表的順序存儲是指用一組地址連續(xù)的存儲單元依次存儲線性表中的各個元素、使得線性表中在邏輯結構上相鄰的數(shù)據(jù)元素存儲在相鄰的物理存儲單元中,即通過數(shù)據(jù)元素物理存儲的相鄰關系來反映數(shù)據(jù)元素之間邏輯上的相鄰關系2021-10-10Java如何獲取List<String>中的String詳解
工作了這么長時間了,一直沒有記錄的習慣,以至于導致我即便是查過的東西總會忘記,下面這篇文章主要給大家介紹了關于Java如何獲取List<String>中String的相關資料,需要的朋友可以參考下2022-02-02springboot開啟Bean數(shù)據(jù)校驗功能
這篇文章主要介紹了springboot開啟Bean數(shù)據(jù)校驗功能,通過啟用Bean屬性校驗導入JSR303與Hibernate校驗框架坐標,使用@Validated注解啟用校驗功能,需要的朋友可以參考下2023-10-10基于Apache組件分析對象池原理的實現(xiàn)案例分析
本文從對象池的一個簡單案例切入,主要分析common-pool2組件關于:池、工廠、配置、對象管理幾個角色的源碼邏輯,并且參考其在Redis中的實踐,對Apache組件分析對象池原理相關知識感興趣的朋友一起看看吧2022-04-04Maven學習----Maven安裝與環(huán)境變量配置教程
這篇文章主要給大家介紹了關于如何利用Maven入手Spring Boot第一個程序的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-06-06