I am trying to generate a single swagger in my microservices project, aggregating all services swaggers into a single one, in the Api Gateway. In order to achieve this, I am following the next tutorial https://objectpartners.com/2017/09/28/aggregate-services-into-a-single-swagger
The problem here is that, when I try to set absolute URLs, the output I am receiving is Failed to load API definition. undefined http://localhost:8070/apihttp://localhost:8081/api/v2/api-docs where localhost:8070/api is the base URL for the api gateway, and localhost:8081/api/v2/api-docs is the docs URL of the swagger of the microservice.
Here is my code:
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;
}
}
Resource
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;
}
}
Finally, the 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
And a couple of images that can help to understand the problem:
Api Gateway Swagger URL
Microservice api-docs URL
For example in spring (springfox-swagger) you need just to put the same tag on multiple API classes and it will merge them in one group in the swagger UI. @Api(value = "First API", tags = {"first-api"}) public class FirstApi { ... } @Api(tags = {"first-api"}) public class SecondApi { ... }
The problem here was just the springfox version... I tried to downgrade it from 2.8.0 to 2.7.0 and it worked like a charm. It seems it is a recognized bug, as you can see here: https://github.com/springfox/springfox/issues/2235
I also had to enable cors in microservices but that is another problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With