网关中心与文档中心整合,首先在micro-service-api-gateway微服务工程中加入swagger依赖。
另外在ApplicationMain类上面加入swagger注解@EnableSwagger2Doc,还要加入一个类ApiGatewaySwaggerResourcesProvider。
package com.lpplpp.gateway.app;
import com.spring4all.swagger.EnableSwagger2Doc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
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;
@SpringBootApplication
@EnableZuulProxy
@EnableSwagger2Doc
public class ApplicationMain {
public static void main(String[] args) {
SpringApplication.run(ApplicationMain.class, args);
System.out.println("gateway microservice is running");
}
@Component
@Primary
class ApiGatewaySwaggerResourcesProvider implements SwaggerResourcesProvider {
@Override
public List get() {
List resource=new ArrayList<>();
resource.add(swaggerResource("product-service","/product-service/v2/api-docs?token=1","1.0"));
resource.add(swaggerResource("order-service","/order-service/v2/api-docs?token=1","1.0"));
return resource;
}
private SwaggerResource swaggerResource(String name,String location,String version){
SwaggerResource swaggerResource=new SwaggerResource();
swaggerResource.setName(name);
swaggerResource.setLocation(location);
swaggerResource.setSwaggerVersion(version);
return swaggerResource;
}
}
}
重新启动网关服务,访问swagger-ui.html地址查看API文档中心,可以看到已经有两个微服务spec文档product-service和order-service。