项目中swagger的使用

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

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

就是当前端访问相应服务的时候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文件中添加配置信息

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

每个模块创建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>

 

关注公众号

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Go 1.17 ,你可以使用 embed 包将静态文件嵌入到可执行文件,因此可以将 Swagger UI 嵌入到 Go Web 项目,而不需要单独部署 Swagger UI。 以下是在 Goland 项目使用 Swagger 的步骤: 1. 安装 Swagger 相关依赖:在项目添加 `github.com/swaggo/swag` 和 `github.com/swaggo/gin-swagger` 两个依赖库,可以使用以下命令: ``` go get -u github.com/swaggo/swag go get -u github.com/swaggo/gin-swagger go get -u github.com/swaggo/gin-swagger/swaggerFiles ``` 2. 在项目添加 Swagger 注释:在需要生成 Swagger 文档的接口上添加注释,例如: ``` // @Summary 获取用户信息 // @Description 根据用户ID获取用户信息 // @Tags 用户管理 // @Produce json // @Param id path int true "用户ID" // @Success 200 {object} UserResponse // @Failure 400 {string} string "请求参数错误" // @Router /users/{id} [get] func GetUserByID(c *gin.Context) { // ... } ``` 3. 生成 Swagger 文档:在项目根目录下执行以下命令,生成 Swagger 文档: ``` swag init ``` 该命令会在项目生成一个 `docs` 目录,其包含了 Swagger 文档的 JSON 文件和 HTML 文件。 4. 在项目嵌入 Swagger UI:在项目添加一个 `swagger` 目录,并将 Swagger UI 的静态文件拷贝到该目录。可以从 Swagger 官网(https://swagger.io/tools/swagger-ui/)下载最新的 Swagger UI 版本。 ``` ├── main.go ├── go.mod ├── go.sum ├── docs │ ├── docs.go │ ├── swagger.json │ └── swagger.yaml └── swagger ├── index.html ├── swagger-ui-standalone-preset.js ├── swagger-ui-standalone-preset.js.map ├── swagger-ui.css ├── swagger-ui.css.map ├── swagger-ui.js ├── swagger-ui.js.map └── swagger-ui.min.js ``` 5. 在项目添加 Swagger UI 的路由:在项目添加一个路由,将 Swagger UI 的 HTML 文件和静态文件提供给用户访问,例如: ``` router.GET("/swagger/*any", gin.WrapH(http.FileServer(http.Dir("./swagger")))) ``` 这样,用户可以通过访问 `/swagger/index.html` 来查看 Swagger 文档。 6. 启动项目并访问 Swagger UI:在 Goland 启动项目,然后在浏览器访问 `http://localhost:8080/swagger/index.html`,即可访问 Swagger UI 并查看文档。 需要注意的是,这种方式虽然可以将 Swagger UI 嵌入到可执行文件,但是每次修改 Swagger UI 后都需要重新编译可执行文件,因此不建议在生产环境使用。如果你需要在生产环境使用 Swagger UI,建议单独部署一个 Swagger UI 服务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值