Spring Cloud 基于zuul 网关整合Swagger的文档规范
内容介绍:
spring cloud 的项目里分多个模块(服务),写文档时不可能每一个服务的文档都是分开的吧,
所以各大网友大神的描述讲解下,我借鉴了网上的大神资料自己整理出了基于Zuul 的项目Swagger文档。
引入项目依赖
需要写文档的模块(服务)都要引入依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
在zuull网关写个文档配置
借鉴网上大神的
@Component
@Primary
class DocumentationConfig implements SwaggerResourcesProvider {
/**
* RouteLocator可以根据zuul配置的路由列表获取服务
*/
private final RouteLocator routeLocator;
public DocumentationConfig(RouteLocator routeLocator) {
this.routeLocator = routeLocator;
}
@Override
public List<SwaggerResource> get() {
List resources = new ArrayList();
//这是添加zuul网关服务,一般时不需要的,一般zuul 服务没有接口。
//resources.add(swaggerResource("default(平台)","/v2/api-docs","2.0"));
List<Route> routes = routeLocator.getRoutes();
//通过RouteLocator获取路由配置,遍历获取所配置服务的接口文档,这样不需要手动添加,实现动态获取
for (Route route: routes) {
resources.add(swaggerResource(route.getId(), route.getFullPath().replace("**", "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;
}
}
写个Spring cloud 项目时我们一般都会单独写个服务来进行zuul 网关注册,前端可以通过这个服务进行调用其他服务的接口,
zuul 的 .yml 如下
zuul:
ignored-services: '*'
routes:
user-service:
path: /userServer/**
service-id: gamebox-user-server
game-service:
path: /gameServer/**
service-id: gamebox-game-server
admin-service:
path: /adminServer/**
service-id: gamebox-admin-server
eureka:
instance:
prefer-ip-address: true
client:
service-url:
defaultZone: http://localhost:1105/eureka/
配置SwaggerConfig
需要写文档的服务可以进行配置SwaggerConfig类
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.gameboxplatformserver.controller"))
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("游戏宝盒->平台模块")
.description("游戏宝盒的平台模块接口文档说明")
.version("2.0")
.build();
}
@Bean
UiConfiguration uiConfig() {
return new UiConfiguration(null, "list", "alpha", "schema",
UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS, false, true, 60000L);
}
}
到这里配置已经完成。下面需要写的时接口的Swagger的注解
@Api("平台")
@RestController
@RequestMapping("/game")
public class GameController {
@Autowired
private GameService gameService;
/**
*平台显示游戏
* @return
*/
@ApiOperation("平台显示")
@PostMapping("/listGame")
public Result listGame(){
List<Game> list = gameService.listGame();
return ResultUtil.success(list);
}
}
访问是通过zuul 网关服务的端口访问 eg:http://localhost:80/swagger-ui.html#/
主要借鉴这么大佬的
https://blog.csdn.net/qq_31489805/article/details/80444284?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.control