当我使用 springfox-swagger 2.9.0 时,我在我的项目中使用了下面的代码。
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
Docket docket = null;
try{
if(!(profile.contains("local")|| (profile.contains("test"))
docket = new Docket(DocumentationType.SWAGGER_2)
.host(host)
.pathProvider(new RelativePathProvider(servletContext){
@Override
public String getApplicationBasePath(){
return "/api";
}
})
.select()
.apis(RequestHandlerSelectors.basePackage("org.app.controller"))
.paths(PathSelectors.any())
.build();
}
else{
docket = new Docket(DocumentationType.SWAGGER_2)
.host(host)
.select()
.apis(RequestHandlerSelectors.basePackage("org.app.controller"))
.paths(PathSelectors.any())
.build();
}
}
catch(Exception e){
logger.info("Unable to return docket",ex)
}
return docket;
}
}
添加以下 swagger 3.0.0 依赖项后,我更新的类是:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
@Configuration
public class SwaggerConfig {
@Bean
public Docket api() {
Docket docket = null;
try{
if(!(profile.contains("local")|| (profile.contains("test"))
docket = new Docket(DocumentationType.SWAGGER_2)
.host(host)
.pathProvider(new PathProvider(){
@Override
public String getOperationPath(String operationPath){
return operationPath.replace("/api","");
}
@Override
public String getResourceListingPath(String groupName, String apiDeclaration){
return null;
}
})
.select()
.apis(RequestHandlerSelectors.basePackage("org.app.controller"))
.paths(PathSelectors.any())
.build();
}
else{
docket = new Docket(DocumentationType.SWAGGER_2)
.host(host)
.select()
.apis(RequestHandlerSelectors.basePackage("org.app.controller"))
.paths(PathSelectors.any())
.build();
}
}
catch(Exception e){
logger.info("Unable to return docket",ex)
}
return docket;
}
}
使用此代码后,我无法从更新的 swagger url 将“/api”附加到我的 baseurl“localhost:8080”。http://localhost:8080/abc-api/swagger-ui/index.html#/
基本 url 应显示为“localhost:8080/api”。
我尝试为 PathProvider 实现创建单独的 bean,然后传入参数,但仍然存在同样的问题。
谁能告诉我我在这里做错了什么以及如何将baseurl创建为“/api”或“/”?