我正试图在我的微服务项目中生成一个招摇,在Api网关中将所有服务swaggers聚合成一个。为了实现这一目标,我将关注下一个教程https://objectpartners.com/2017/09/28/aggregate-services-into-a-single-swagger
这里的问题是,当我尝试设置绝对URL时,我收到的输出是 无法加载API定义。 undefined http://localhost:8070/apihttp://localhost:8081/api/v2/api-docs 其中 localhost:8070 / api 是api网关的基本URL, localhost:8081 / api / v2 / api- docs 是微服务招摇的文档URL。
这是我的代码:
SwaggerConfiguration
package com.rfd.apigateway.swagger;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import java.util.List;
@Configuration
@ConfigurationProperties(prefix = "swagger")
public class SwaggerConfiguration {
private List<Resource> resources;
public List<Resource> getResources() {
return resources;
}
public void setResources(List<Resource> resources) {
this.resources = resources;
}
}
资源
package com.rfd.apigateway.swagger;
public class Resource {
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;
}
}
DocumentationController
package com.rfd.apigateway.swagger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;
import java.util.ArrayList;
import java.util.List;
@Component
@Primary
@EnableAutoConfiguration
public class DocumentationController implements SwaggerResourcesProvider {
private SwaggerConfiguration swaggerConfiguration;
@Autowired
public DocumentationController(SwaggerConfiguration swaggerConfiguration){
this.swaggerConfiguration = swaggerConfiguration;
}
@Override
public List get() {
List resources = new ArrayList<>();
for(Resource resource : this.swaggerConfiguration.getResources()){
resources.add(createSwaggerResource(resource));
}
return resources;
}
private SwaggerResource createSwaggerResource(Resource resource) {
SwaggerResource swaggerResource = new SwaggerResource();
swaggerResource.setName(resource.getName());
swaggerResource.setUrl(resource.getUrl());
swaggerResource.setSwaggerVersion(resource.getVersion());
return swaggerResource;
}
}
最后, application.yml
swagger:
resources:
- name: transactions
url: http://localhost:8081/api/v2/api-docs
version: 1.0
- name: payments
url: http://localhost:8083/api/v2/api-docs
version: 1.0
以及一些有助于理解问题的图片:
答案 0 :(得分:0)
这里的问题只是springfox版本......我试图将它从2.8.0降级到2.7.0,它就像一个魅力。它似乎是一个公认的错误,你可以在这里看到:https://github.com/springfox/springfox/issues/2235
我还必须在微服务中启用cors,但这是另一个问题。
答案 1 :(得分:0)
我能够通过使用https://github.com/varghgeorge/microservices-single-swagger
来解决它这个简单的sprintboot微服务可以在一个地方显示所有swagger文档。基于YAML配置,它将显示服务列表,并为所有服务提供单个文档服务器。在YAML配置中,您可以添加不同的招摇网址。
答案 2 :(得分:-1)
尝试一下:https://github.com/varghgeorge/microservices-single-swagger
来自不同Swagger的简单服务器聚合