本文主要是介绍Spring Cloud Gateway聚合Swagger2文档(初始版),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在spring Cloud架构下,由于各个微服务拥有独立的Swagger接口文档,但我们不可能每次零散地通过各个服务的端口去访问需要查看的服务接口文档,所以为了能更加直观方便地查看,我们可以在Spring Cloud Gateway网关模块聚合各微服务的Swagger接口文档。
依赖版本:
依赖 | 版本 |
---|---|
Spring Boot | 2.3.0.RELEASE |
Spring Cloud | Hoxton.SR4 |
springfox-swagger2 | 2.8.0 |
springfox-swagger-ui | 2.8.0 |
这里只贴关键代码:
admin-user用户服务:
swagger配置文件:
import com.fasterxml.classmate.GenericType;
import com.fasterxml.classmate.TypeResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.web.context.request.async.DeferredResult;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.WildcardType;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;import java.util.*;import static springfox.documentation.schema.AlternateTypeRules.newRule;/*** swagger接口文档配置* @Author: ZJH* @Date: 2019/12/5 10:15*/@Configuration
@EnableSwagger2
public class SwaggerConfig {// swagger接口界面访问路径 :http://localhost:9800/swagger-ui.html IP为机器的IP,端口号为工程的端口@Autowiredprivate TypeResolver typeResolver;@Beanpublic Docket api() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()// api接口路径,即control
这篇关于Spring Cloud Gateway聚合Swagger2文档(初始版)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!