可能出現(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>