背景:
在前后端分离的大趋势下,swagger这种强大RESTful API文档在前端开发与后端开发的联调过程中有着不可或缺的作用。由于一个微服务体系里面往往存在多个微服务,正常我们访问其中一个微服务的swagger文档的路径格式是:ip+port/doc.html。但是如果我们每看一个微服务的API文档都需要切换ip和端口,就变得比较麻烦。于是在这里小编给大家整理一份在网关zuul整合一个微服务体系下的所有swagger文档教程。
一 springboot整合swagger
首先需要在每个微服务配置swagger的相应配置。比如第一步我们需要引入swagger的依赖。
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
接下来我们需要添加配置类
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
import java.util.List;
/**
* swagger配置类
*
* @author mark
* @date 2022年12月10日 11:37:07
*/
/*@Profile({"dev", "test"}) // 仅本地测试、测试环境