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)這個那個問題:
問題一:提示沒有權(quá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 {
}總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java依賴jave-all-deps實現(xiàn)視頻格式轉(zhuǎn)換
jave-all-deps是一款基于FFmpeg庫的Java音視頻編解碼庫,本文主要介紹了java依賴jave-all-deps實現(xiàn)視頻格式轉(zhuǎn)換,具有一定的參考價值,感興趣的可以了解一下2024-07-07
Spring Boot Controller處理HTTP請求體的方法
Spring Boot提供了強大的機制來處理不同 Content-Type? 的HTTP請求體,這主要依賴于 HttpMessageConverter? 接口的各種實現(xiàn),它們能夠自動將請求體內(nèi)容轉(zhuǎn)換成Java方法參數(shù),本文給大家介紹Spring Boot Controller處理HTTP請求體的方法,感興趣的朋友一起看看吧2025-05-05
論java如何通過反射獲得方法真實參數(shù)名及擴展研究
這篇文章主要為大家介紹了java如何通過反射獲得方法的真實參數(shù)名以及擴展研究,有需要的朋友可以借鑒參考下,希望能夠有所幫助祝大家多多進步早日升職加薪2022-01-01
全網(wǎng)最精細詳解二叉樹,2萬字帶你進入算法領(lǐng)域
大家好,我是哪吒,一個熱愛編碼的Java工程師,本著"欲速則不達,欲達則欲速"的學(xué)習(xí)態(tài)度,在程序猿這條不歸路上不斷成長,所謂成長,不過是用時間慢慢擦亮你的眼睛,少時看重的,年長后卻視若鴻毛,少時看輕的,年長后卻視若泰山,成長之路,亦是漸漸放下執(zhí)念,內(nèi)心歸于平靜的旅程2021-08-08

