项目中swagger的使用

在项目中我们的API文档有swagger来进行管理,每个服务模块都配有swagger来进行接口管理

那么怎么实现swagger的同一管理呢,我们可以使用zuul网关来实现

项目中swagger的使用

项目中swagger的使用

就是当前端访问相应服务的时候zull网关会根据服务ip去调响应的服务模块的swaggerAPI,

在项目中的使用方法

在每个模块中添加swagger的依赖

<!-- swagger2 -->

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger2</artifactId>

<version>2.8.0</version>

</dependency>

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger-ui</artifactId>

<version>2.8.0</version>

2.在yml文件中添加配置信息

项目中swagger的使用

3.在控制层开启swagger注解,并使用@API添加接口的描述

项目中swagger的使用

每个模块创建swaggerconfig

@Configuration

@EnableSwagger2

public class SwaggerConfig {

 

@Bean

public Docket createRestApi() {

return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()

// api扫包

.apis(RequestHandlerSelectors.basePackage("com.itmayiedu.api")).paths(PathSelectors.any()).build();

}

 

private ApiInfo apiInfo() {

return new ApiInfoBuilder().title("微服务电商系统").description("分布式&微服务")

.termsOfServiceUrl("http://www.itmayiedu.com")

// .contact(contact)

.version("1.0").build();

}

 

}

 

Zull整合Swagger管理微服务所有API

会员和订单引入Maven依赖

<!-- swagger-spring-boot -->

<dependency>

<groupId>com.spring4all</groupId>

<artifactId>swagger-spring-boot-starter</artifactId>

<version>1.7.0.RELEASE</version>

</dependency>

 

application.yml配置

Api接口扫描范围

swagger:

  base-package: com.itmayeidu.api

 

 

项目启动引入开启生成文档

@EnableSwagger2Doc

 

 

 

ZuulGateway网关

@SpringBootApplication

@EnableEurekaClient

@EnableZuulProxy

@EnableSwagger2Doc

public class AppGateWay {

 

// @EnableZuulProxy 开启网关代理

 

public static void main(String[] args) {

SpringApplication.run(AppGateWay.class, args);

}

 

// zuul配置能够使用config实现实时更新

@RefreshScope

@ConfigurationProperties("zuul")

public ZuulProperties zuulProperties() {

return new ZuulProperties();

}

 

// 添加文档来源

@Component

@Primary

class DocumentationConfig implements SwaggerResourcesProvider {

@Override

public List<SwaggerResource> get() {

List resources = new ArrayList<>();

// app-itmayiedu-order

resources.add(swaggerResource("app-itmayiedu-member", "/api-member/v2/api-docs", "2.0"));

resources.add(swaggerResource("app-itmayiedu-order", "/api-order/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;

}

}

 

}

 

 

 

 

Maven依赖信息

<dependency>

<groupId>com.spring4all</groupId>

<artifactId>swagger-spring-boot-starter</artifactId>

<version>1.7.0.RELEASE</version>

</dependency>

 

项目中swagger的使用