在不同命名空间中具有相同名称的Swagger不起作用是因为它们的路由和定义冲突。为了解决这个问题,你可以使用以下解决方法之一:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket swaggerApi1() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("namespace1")
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.namespace1.controllers"))
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}
@Bean
public Docket swaggerApi2() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("namespace2")
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.namespace2.controllers"))
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
// 设置Swagger的基本信息
}
}
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Autowired
private ServletContext servletContext;
@Bean
public Docket swaggerApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.controllers"))
.paths(PathSelectors.ant("/namespace1/**")) // 设置命名空间1的路由前缀
.build()
.apiInfo(apiInfo())
.pathProvider(new RelativePathProvider(servletContext) {
@Override
public String getApplicationBasePath() {
return "/namespace1"; // 设置Swagger UI的上下文路径
}
});
}
private ApiInfo apiInfo() {
// 设置Swagger的基本信息
}
}
请根据你的具体情况选择适合的解决方法,并根据你的项目结构和命名空间配置进行相应的调整。