Using zuul to aggregate multiple swagger api documents of microservices into one document

Posted by etully on Sun, 05 Jan 2020 04:47:00 +0100

First, look at the renderings:

Myc order: on behalf of order service

Myc user: on behalf of user service

Myc car: representative car service

... and so on.

Let me briefly talk about the integration steps and key points.

1. Add the following dependencies to the pom of each service.


Note: just add this. zuul is responsible for ui.

2. Put this in your config to start. Note that here I use a placeholder to get the name of the current document to avoid the possibility of adding other modules later.

@ConditionalOnClass(value = {Swagger.class})
public class SwaggerConfig {
    private String applicationName;
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(applicationName+"Interface document")
                .description(applicationName+"Interface document")
                .contact(new Contact("miaoyouche", "", ""))

    private List<Parameter> parameters() {
        ParameterBuilder parameterBuilder = new ParameterBuilder();
        List<Parameter> parameters = new ArrayList<>();"Authorization")
                .modelRef(new ModelRef("string"))
        return parameters;


Then zuul's configuration

3. Add the following configuration in zuul's config. Note that there is an apiNames that is all the grouping service names. Avoid write dead and read directly from the configuration file

public class DocumentationConfig implements SwaggerResourcesProvider {
    private String[] apiNames;

    public List<SwaggerResource> get() {
        List resources = new ArrayList<>();
        if (apiNames != null) {
                    resources.add(swaggerResource(s, "/openapi/" + s + "/v2/api-docs", "2.0"))
        return resources;

    private SwaggerResource swaggerResource(String name, String location, String version) {
        SwaggerResource swaggerResource = new SwaggerResource();
        return swaggerResource;

4.zuul's pom file adds the following dependencies:


5.zuul's agent configuration:

      path: /openapi/myc-user/**
      serviceId: myc-user
      path: /openapi/myc-car/**
      serviceId: myc-car
      path: /openapi/myc-auth/**
      serviceId: myc-auth
      path: /openapi/myc-order/**
      serviceId: myc-order
  stripPrefix: true

Here's how to access the startup project:


Topics: Spring REST github