背景
项目上会把一个后端接口映射为如下结构
{
desc: "获取英雄详情",
method: "post",
name: "getHeroInfo",
url: "/api/getHeroInfo",
},
复制代码
如果一次新增了10个接口,那么就需要,Ctrl+c、Ctrl+v、切屏…
方案
一开始想用无头浏览器来着,看了一下文档请求完全不需要
发起请求: http://10.200.0.3:59080/swagger-resources
获取有哪些组
[
{
name: "background-admin",
url: "/v2/api-docs?group=background-admin",
swaggerVersion: "2.0",
location: "/v2/api-docs?group=background-admin",
},
]
复制代码
发起请求 http://10.200.0.3:59080/v2/api-docs?group=doctor-PC ,获取组下面有哪些接口,可以说页面上的所有内容都是通过接口生成的,简易结构如下
{
basePath: "/",
definitions: {
"ResultVO«英雄详细信息»": {
type: "object",
properties: {
data: {
description:
"响应数据:成功时返回需要的数据,失败时返回详细原因或为null",
$ref: "#/definitions/英雄详细信息",
},
message: {
type: "string",
description: "请求状态描述",
},
status: {
type: "integer",
format: "int32",
description: "请求状态码,200-正确,其它-错误",
},
},
title: "ResultVO«web医生服务消息配置»",
},
英雄详细信息: {
type: "object",
properties: {
name: {
type: "string",
description: "英雄姓名",
},
age: {
format: "int32",
type: "integer",
description: "英雄年龄",
},
birthday: {
type: "string",
format: "date-time",
description: "英雄出生时间",
},
live: {
type: "boolean",
description: "现在是否还活着",
},
},
},
},
paths: {
"/api/getHeroInfo": {
post: {
tags: ["英雄信息查询接口"],
summary: "英雄生平",
parameters: [
{
name: "id",
in: "query",
required: true,
type: "string",
},
],
responses: {
200: {
description: "OK",
$ref: "#/definitions/ResultVO«英雄详细信息»",
},
20107: {
description: "参数异常",
},
},
deprecated: false,
},
},
},
}
复制代码
自动映射
根据summary
属性去匹配,就可以一次性映射该类目下的所有接口
自动生成mock数据
先获取所有的组,依次遍历组下面所有的接口
$ref: “#/definitions/ResultVO«英雄详细信息»”, 所有的结构体都定义在definitions
,是互相嵌套的,需要递归去组装结构体
每个参数都有对应的描述
birthday: {
type: "string",
format: "date-time",
description: "英雄出生时间",
},
复制代码
可以借助 Mock.Random,结合具体的项目,生成比Swagger文档上更直观的”响应示例”,比如status
的值为200,具体应用可参考 github.com/xiaodun/sf-…
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END