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")) // 為當(dāng)前包路徑
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Swagger3 RESTful API") // 頁面標(biāo)題
.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)配置到你的項(xià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的錯誤,這是因?yàn)榻y(tǒng)一返回體影響到了Swagger3的一些內(nèi)置接口。
解決方法是??
@RestControllerAdvice???控制好生效的包范圍,在你的實(shí)現(xiàn)類上加上basePackages = “項(xiàng)目包路徑”
@ControllerAdvice(basePackages = "com.wanghan")
public class ApiResBodyAdvice implements ResponseBodyAdvice {
}總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java 數(shù)據(jù)結(jié)構(gòu)線性表之順序存儲詳解原理
線性表的順序存儲是指用一組地址連續(xù)的存儲單元依次存儲線性表中的各個元素、使得線性表中在邏輯結(jié)構(gòu)上相鄰的數(shù)據(jù)元素存儲在相鄰的物理存儲單元中,即通過數(shù)據(jù)元素物理存儲的相鄰關(guān)系來反映數(shù)據(jù)元素之間邏輯上的相鄰關(guān)系2021-10-10
Java如何獲取List<String>中的String詳解
工作了這么長時間了,一直沒有記錄的習(xí)慣,以至于導(dǎo)致我即便是查過的東西總會忘記,下面這篇文章主要給大家介紹了關(guān)于Java如何獲取List<String>中String的相關(guān)資料,需要的朋友可以參考下2022-02-02
springboot開啟Bean數(shù)據(jù)校驗(yàn)功能
這篇文章主要介紹了springboot開啟Bean數(shù)據(jù)校驗(yàn)功能,通過啟用Bean屬性校驗(yàn)導(dǎo)入JSR303與Hibernate校驗(yàn)框架坐標(biāo),使用@Validated注解啟用校驗(yàn)功能,需要的朋友可以參考下2023-10-10
Java中IO流文件讀取、寫入和復(fù)制的實(shí)例
下面小編就為大家?guī)硪黄狫ava中IO流文件讀取、寫入和復(fù)制的實(shí)例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
基于Apache組件分析對象池原理的實(shí)現(xiàn)案例分析
本文從對象池的一個簡單案例切入,主要分析common-pool2組件關(guān)于:池、工廠、配置、對象管理幾個角色的源碼邏輯,并且參考其在Redis中的實(shí)踐,對Apache組件分析對象池原理相關(guān)知識感興趣的朋友一起看看吧2022-04-04
java中char對應(yīng)的ASCII碼的轉(zhuǎn)化操作
這篇文章主要介紹了java中char對應(yīng)的ASCII碼的轉(zhuǎn)化操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
Maven學(xué)習(xí)----Maven安裝與環(huán)境變量配置教程
這篇文章主要給大家介紹了關(guān)于如何利用Maven入手Spring Boot第一個程序的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06

