swagger consolidation for multiple microservices(多个微服务的大摇大摆整合)
问题描述
I have multiple microservices, for which swagger has already been implemented. I would like to bring all the api's under single swagger UI. I've followed the following link for doing this. but tried it in maven approach in STS. Swagger Consilidation Github example
Here are my different files in the project,
@SpringBootApplication
@ComponentScan
@EnableAutoConfiguration
@EnableSwagger2
public class SgtestApplication {
public static void main(String[] args) {
SpringApplication.run(SgtestApplication.class, args);
}
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
.apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.boot")))
.apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.cloud")))
.apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.security")))
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Single swagger")
.description("API to retrieve swagger apis")
.version("1.0.0")
.build();
}
}
My resource provider is as follows,
@Component
@Primary
@EnableAutoConfiguration
public class GatewaySwaggerResourceProvider implements SwaggerResourcesProvider {
@Autowired
private SwaggerServicesConfig swaggerServiceList;
public GatewaySwaggerResourceProvider() {
}
@Override
public List<SwaggerResource> get() {
List<SwaggerResource> resources = new ArrayList<>();
List<SwaggerServices> servList=swaggerServiceList.getServiceList();
for (SwaggerServices swaggerServices : servList) {
resources.add(swaggerResource(swaggerServices.getName(), swaggerServices.getUrl(),swaggerServices.getVersion()));
}
/*swaggerServiceList.getServiceList().forEach(service -> {
resources.add(swaggerResource(service.getName(), service.getUrl(), service.getVersion()));
});*/
return resources;
}
private SwaggerResource swaggerResource(String name, String location, String version) {
SwaggerResource swaggerResource = new SwaggerResource();
swaggerResource.setName(name);
swaggerResource.setLocation(location);
swaggerResource.setSwaggerVersion(version);
return swaggerResource;
}
}
and finally my service configuration,
@Component
@EnableAutoConfiguration
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "documentation.swagger")
public class SwaggerServicesConfig {
List<SwaggerServices> swagger;
public List<SwaggerServices> getServiceList() {
return swagger;
}
public void setServiceList(List<SwaggerServices> swaggerResources) {
this.swagger = swaggerResources;
}
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "documentation.swagger.services")
public static class SwaggerServices {
private String name;
private String url;
private String version;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
@Override
public String toString() {
return "SwaggerServices [name=" + name + ", url=" + url + ", version=" + version + "]";
}
}
}
in my application.yml i am specifying the api docs endpoint of different microservices.
spring:
profiles: default
server:
port: 8014
documentation:
swagger:
service-list:
- name: local-swagger
url: http://localhost:8085/v2/api-docs
version: 1.0
and my output is as follows,
could someone help me out by what am i exactly doing wrong here? i am able to get swager ui but it is not showng any list of api's in that. I am new to swagger, so please kindly review my program.
I think there is a CORS ERROR. Please specify the CORS origin header for all of your Swagger documentation services.
For Better Approach: Instead of specifying micro-services URL explicitly. Register all services to Service register and get all your instance details from it. This is the better approach for centralised swagger.
For Reference: Centralized Swagger Documentation
这篇关于多个微服务的大摇大摆整合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:多个微服务的大摇大摆整合
基础教程推荐
- Java ECDSAwithSHA256 签名长度不一致 2022-01-01
- 将 double 转换为 Int,向下舍入 2022-01-01
- doFilter()是在servlet的工作完成之前还是之后执行的? 2022-01-01
- 如何在相机中应用自定义滤镜 [Surfaceview 预览]. 2022-01-01
- JPA惰性列表上的流 2022-01-01
- 在java中使用xpath和selenium解析HTML表格数据 2022-01-01
- Maven:无效的目标版本:10 2022-01-01
- 控制台应用程序中的 Java 键盘输入解析 2022-01-01
- 将 Windows 证书导入 Java 2022-01-01
- 在springboot中如何给mybatis加拦截器 2023-04-29
