1.在Swagger Editor上编写基于OpenApi的接口文档
OpenApi官方文档地址:点击打开链接
Swagger编辑器地址:swagger editor
Prism模拟服务器地址:http://stoplight.io/platform/prism/
swagger: "2.0"
info:
version: 1.0.0
title: Simple API
description: A simple API to learn how to write OpenAPI Specification
schemes:
- http
host: localhost:4010
basePath: /
paths:
/user:
get:
summary: Gets some persons
tags:
- "user"
produces:
- "application/json"
description: Returns a list containing all persons. The list supports paging.
responses:
200:
description: A list of Person
schema:
type: array
items:
required:
- id
properties:
id:
type: string
username:
type: string
password:
type: string
post:
tags:
- "user"
summary: "add user"
description: ""
produces:
- "application/xml"
- "application/json"
parameters:
- in: "body"
name: "body"
description: "order placed for purchasing the pet"
required: true
schema:
$ref: "#/definitions/User"
responses:
200:
description: "successful operation"
schema:
$ref: "#/definitions/User"
400:
description: "Invalid Order"
put:
tags:
- "user"
summary: "update user"
description: "update user"
produces:
- "application/xml"
- "application/json"
parameters:
- in: "body"
name: "body"
description: "order placed for purchasing the pet"
required: true
schema:
$ref: "#/definitions/User"
responses:
200:
description: "successful operation"
schema:
$ref: "#/definitions/User"
400:
description: "Invalid Order"
/user/{id}:
get:
summary: Gets a person
tags:
- "user"
produces:
- "application/xml"
- "application/json"
description: Returns a single person for its username.
parameters:
- name: id
in: path
required: true
description: The person's id
type: integer
responses:
200:
description: A Person
schema:
required:
- id
properties:
username:
type: string
password:
type: string
id:
type: integer
404:
description: The Person does not exists.
delete:
summary: delete a person
tags:
- "user"
produces:
- "application/xml"
- "application/json"
description: Returns a single person for its username.
parameters:
- name: id
in: path
required: true
description: The person's id
type: integer
responses:
200:
description: delete user
404:
description: The Person does not exists.
definitions:
User:
type: "object"
properties:
id:
type: "integer"
format: "int64"
username:
type: "string"
password:
type: "string"
xml:
name: "User"
点击编辑器左上方的file生成Json文件保存到本地。
2.运行Prism
进入Prism的文件目录,运行Prism 使用--spec将上面导出的Json文件导入,使用--cors支持跨域
3.测试结果
在Swagger Editor界面进行测试,并成功返回Mock数据