springboot整合swagger3報(bào)Unable to infer base url錯(cuò)誤問(wèn)題
springboot整swagger3
工程中的pom文件加入依賴(lài)包
<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;
/**
* 說(shuō)明: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") // 頁(yè)面標(biāo)題
.version("3.0") // 版本號(hào)
.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;
/**
* 說(shuō)明: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)目中了。
可以通過(guò)url訪(fǎng)問(wèn)了:
http://localhost:8080/swagger-ui/index.html
總是不那么順利
然而在使用過(guò)程中,總是出現(xiàn)這個(gè)那個(gè)問(wèn)題:
問(wèn)題一:提示沒(méi)有權(quán)限訪(fǎng)問(wèn)
如果你使用安全框架,Swagger3的內(nèi)置接口就會(huì)訪(fǎng)問(wèn)受限,我們需要排除掉。
Spring Security是這么配置的:
@Override
public void configure(WebSecurity web) throws Exception {
//忽略swagger3所需要用到的靜態(tài)資源,允許訪(fǎng)問(wèn)
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/**"});
};
}問(wèn)題二:報(bào)??Unable to infer base url
你會(huì)發(fā)現(xiàn)Swagger3會(huì)報(bào)??Unable to infer base url的錯(cuò)誤,這是因?yàn)榻y(tǒng)一返回體影響到了Swagger3的一些內(nèi)置接口。
解決方法是??
@RestControllerAdvice???控制好生效的包范圍,在你的實(shí)現(xiàn)類(lèi)上加上basePackages = “項(xiàng)目包路徑”
@ControllerAdvice(basePackages = "com.wanghan")
public class ApiResBodyAdvice implements ResponseBodyAdvice {
}總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java依賴(lài)jave-all-deps實(shí)現(xiàn)視頻格式轉(zhuǎn)換
jave-all-deps是一款基于FFmpeg庫(kù)的Java音視頻編解碼庫(kù),本文主要介紹了java依賴(lài)jave-all-deps實(shí)現(xiàn)視頻格式轉(zhuǎn)換,具有一定的參考價(jià)值,感興趣的可以了解一下2024-07-07
Spring Boot Controller處理HTTP請(qǐng)求體的方法
Spring Boot提供了強(qiáng)大的機(jī)制來(lái)處理不同 Content-Type? 的HTTP請(qǐng)求體,這主要依賴(lài)于 HttpMessageConverter? 接口的各種實(shí)現(xiàn),它們能夠自動(dòng)將請(qǐng)求體內(nèi)容轉(zhuǎn)換成Java方法參數(shù),本文給大家介紹Spring Boot Controller處理HTTP請(qǐng)求體的方法,感興趣的朋友一起看看吧2025-05-05
論java如何通過(guò)反射獲得方法真實(shí)參數(shù)名及擴(kuò)展研究
這篇文章主要為大家介紹了java如何通過(guò)反射獲得方法的真實(shí)參數(shù)名以及擴(kuò)展研究,有需要的朋友可以借鑒參考下,希望能夠有所幫助祝大家多多進(jìn)步早日升職加薪2022-01-01
全網(wǎng)最精細(xì)詳解二叉樹(shù),2萬(wàn)字帶你進(jìn)入算法領(lǐng)域
大家好,我是哪吒,一個(gè)熱愛(ài)編碼的Java工程師,本著"欲速則不達(dá),欲達(dá)則欲速"的學(xué)習(xí)態(tài)度,在程序猿這條不歸路上不斷成長(zhǎng),所謂成長(zhǎng),不過(guò)是用時(shí)間慢慢擦亮你的眼睛,少時(shí)看重的,年長(zhǎng)后卻視若鴻毛,少時(shí)看輕的,年長(zhǎng)后卻視若泰山,成長(zhǎng)之路,亦是漸漸放下執(zhí)念,內(nèi)心歸于平靜的旅程2021-08-08

