分三块介绍:
一、单体应用集成swagger
二、zuul网关集成swagger
三、gateway网关集成swagger集成spring security oauth2
标题单体应用集成swagger
一、pom添加坐标
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.9.1.RELEASE</version>
</dependency>
二、启动类加开启注解
@EnableSwagger2Doc
三、配置文件添加配置
server:
port: 9001
swagger:
base-package: com.funtl.myshop.plus.business
title: SpringCloud2.x构建XXXX项目-会员服务接口
description: 该项目。。。。。。。
version: 1.1
terms-of-service-url: www.baidu.com
contact:
name: 作者名字
email: [email protected]
enabled: true
authorization:
key-name: Authorization
四、访问链接
localhost:9001/swagger-ui.html
zuul网关集成swagger
在单体应用集成的基础上,配置zuul网关集成swagger。
一、在网关pom添加坐标
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.9.1.RELEASE</version>
</dependency>
二、在网关启动类加开启注解
@EnableSwagger2Doc
三、在网关配置文件中配置路由
##服务启动端口号
server:
port: 8080
## 配置网关反向代理
zuul:
routes:
api-a:
### 以 /api-weixin/访问转发到会员服务
path: /api-weixin/**
serviceId: app-mayikt-weixin
api-b:
### 以 /api-member/访问转发到订单服务
path: /api-member/**
serviceId: app-mayikt-member
四、在启动类上添加配置
// 添加文档来源
@Component
@Primary
class DocumentationConfig implements SwaggerResourcesProvider {
@Override
public List<SwaggerResource> get() {
List resources = new ArrayList<>();
// app-itmayiedu-order
// 网关使用服务别名获取远程服务的SwaggerApi
resources.add(swaggerResource("app-mayikt-member", "/app-mayikt-member/v2/api-docs", "2.0"));
resources.add(swaggerResource("app-mayikt-weixin", "/app-mayikt-weixin/v2/api-docs", "2.0"));
return resources;
}
private SwaggerResource swaggerResource(String name, String location, String version) {
SwaggerResource swaggerResource = new SwaggerResource();
swaggerResource.setName(name);
swaggerResource.setLocation(location);
swaggerResource.setSwaggerVersion(version);
return swaggerResource;
}
}
五、访问链接
localhost:8080