在我的Springboot项目中添加了swagger3后,无法将应用程序名称添加到基础URL



当我使用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;
}
}

使用此代码后,我无法追加"/api"localhost:8080;从更新swagger url。http://localhost: 8080/abc-api swagger-ui/index . html #/

基础url应该显示为"localhost:8080/api"

我尝试为PathProvider实现创建单独的bean,然后传入参数,但仍然存在相同的问题。

有没有人能告诉我我在这里做错了什么,以及如何创建baseurl为"/api"而不是"/">

?

这不是你的问题的答案,但它可能会帮助你。既然无论如何都要进行迁移,请考虑使用springdoc代替Springfox。它是一个较新的库,比Springfox更容易使用,也更不容易出错。我们两年前搬到了那里,我们很高兴我们这么做了。网上有非常好的文档和教程:

  • https://springdoc.org/
  • https://www.baeldung.com/spring-rest-openapi-documentation

它也是非常活跃的,你通常会得到你的问题回答非常快在github页面。

相关内容

最新更新