2020-05-25:SpringBoot配置Swagger2

可能出現(xiàn)的問題(彈框問題):
Unable to infer base url. This is common when using dynamic servlet registration or when the API is behind an API Gateway. The base url is the root of where all the swagger resources are served. For e.g. if the api is available at http://example.org/api/v2/api-docs then the base url is http://example.org/api/. Please enter the location manually:
出現(xiàn)問題的原因可能是:
1:項目有shiro攔截了
2.在啟動類沒有添加注解:@EnableSwagger2
下面是配置Swagger的過程
1.啟動類添加@EnableSwagger2注解
@SpringBootApplication
@EnableSwagger2
public class Demo1Application {

public static void main(String[] args) {
    SpringApplication.run(Demo1Application.class, args);
}

}
2.在啟動類同級包下創(chuàng)建SwaggerConfig
@Configuration
public class SwaggerConfig {
/**
* 創(chuàng)建API應(yīng)用
* apiInfo() 增加API相關(guān)信息
* 通過select()函數(shù)返回一個ApiSelectorBuilder實例,用來控制哪些接口暴露給Swagger來展現(xiàn),
* 本例采用指定掃描的包路徑來定義指定要建立API的目錄。
*
* @return
*/

@Bean
public Docket createRestApi() {
    return new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo())
            .enable(true)
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.example.demo1"))
            .paths(PathSelectors.any())
            .build();
}

/**
 * 創(chuàng)建該API的基本信息(這些基本信息會展現(xiàn)在文檔頁面中)
 * 訪問地址:http://項目實際地址/swagger-ui.html
 * @return
 */

private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
            .title("測試swagger")
            .version("1.0")
            .build();
}

}
3.添加依賴
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容