使用Swagger生成JAVA Mock Server(Springboot)代码
来源:互联网 发布:淘宝代销商怎么做 编辑:程序博客网 时间:2024/04/30 19:28
Swagger为我们提供了非常多的工具,其中最强的还要算这个代码的生成工具。在前后端分离的大环境下,前后端之间订立的接口显得尤为重要,接口在订立之后变动的可能性已经很小,这就要求我们提前去设计接口,也就是我们为前端提供的API。
但是我们发现,在开发过程中订立的接口寿命其实很短,这是一件非常严重的事情。因此Swagger为我们提供了另外一种比较优雅的方式:就是你先订立接口,然后再去生成接口;也就是使用接口文档去生成代码,这是非常好的一种实践方式。
Mock Server为我们解决下面的问题:
1、我们设计了API,但后台还没有开发完成;
2、因为网络等原因,导致直接调用真实的后台API会很不稳定或者很耗时。
前期准备
先看下面文章,如何生成API文档,拿到文档后,我们才能生成Mock Server Code
SpringBoot中使用Swagger生成RESTful规范API文档
http://blog.csdn.net/a78270528/article/details/78506338
实现步骤:
1、访问:http://localhost:8090/v2/api-docs,会返回接口的JSON数据,复制(文章最后有完整JSON代码)。
2、再打开Swagger Editor(https://editor.swagger.io),也可以下载代码,在本地部署访问。我们这里使用在线方式,粘贴刚刚复制的JSON,提示转成YAML:
3、点击Generate Server,选择spring,直接弹出下载代码。
5、这时候我们可以正常启动项目,Swagger2SpringBoot,右键Run as..
7、我们按照接口要求填写了接口,发送后返回HTTP请求415错误 – 不支持的媒体类型(Unsupported media type)
8、解决方法是去除Swagger生成接口的Consumes,让其不限制请求类型:
10、所以们在发送请求时在Headers中加入Accept: application/json,再次发送请求,这回返回提200,终于成功了:
12、这时我们再次发送接口,返回如下:
到此,我们整个Mock Server的代码已经调通,部署并可以正常使用了。
为什么406会报错,又要我们指定Accept: application/json,这是Swagger的问题,我们需要集成Swagger的源码,修改swagger.js如下:
if (this.type === "POST" || this.type === "GET" || this.type === "PATCH") { if (this.opts.responseContentType) { responseContentType = this.opts.responseContentType; } else { responseContentType = "application/json"; }}
再在swagger-ui.js中替换这一行:
opts.responseContentType = $("div select[name=responseContentType]", $(this.el)).val();
替换成:
if($("div select[name=responseContentType]", $(this.el)).val() === undefined) { opts.responseContentType = opts.parent.model.produces[0];}else { opts.responseContentType = $("div select[name=responseContentType]", $(this.el)).val();}
这样就从根本敢406报错的问题,再请求时就不用加上Accept: application/json了。
下面附上接口的JSON文件,仅供测试使用。
{ "swagger": "2.0", "info": { "description": "2017.11.9上线版本", "version": "1.0", "title": "实战指挥平台--基础数据API说明文档", "contact": { "name": "智慧消防研发部" } }, "host": "127.0.0.1:8090", "basePath": "/", "tags": [ { "name": "validator-controller", "description": "Validator Controller" }, { "name": "user-controller", "description": "测试UserController" } ], "paths": { "/user": { "get": { "tags": [ "user-controller" ], "summary": "查询个人信息接口", "description": "查询个人信息接口", "operationId": "queryUsingGET", "consumes": [ "application/json" ], "produces": [ "*/*" ], "parameters": [ { "name": "page", "in": "query", "description": "page", "required": true, "type": "integer", "format": "int32" }, { "name": "count", "in": "query", "description": "count", "required": true, "type": "integer", "format": "int32" }, { "name": "token", "in": "header", "description": "访问凭证", "required": true, "type": "string" } ], "responses": { "200": { "description": "OK", "schema": { "type": "array", "items": { "$ref": "#/definitions/Demo" } } }, "401": { "description": "Unauthorized" }, "403": { "description": "Forbidden" }, "404": { "description": "Not Found" } } }, "post": { "tags": [ "user-controller" ], "summary": "增加个人信息接口", "description": "增加个人信息接口", "operationId": "insertUsingPOST", "consumes": [ "application/json" ], "produces": [ "*/*" ], "parameters": [ { "in": "body", "name": "name", "description": "name", "required": false, "schema": { "type": "string" } } ], "responses": { "200": { "description": "OK" }, "201": { "description": "Created" }, "401": { "description": "Unauthorized" }, "403": { "description": "Forbidden" }, "404": { "description": "Not Found" } } } }, "/validator/query": { "get": { "tags": [ "validator-controller" ], "summary": "query", "operationId": "queryUsingGET_1", "consumes": [ "application/json" ], "produces": [ "*/*" ], "parameters": [ { "in": "body", "name": "page", "description": "page", "required": false, "schema": { "type": "integer", "format": "int32" } }, { "name": "count", "in": "query", "description": "count", "required": true, "type": "integer", "format": "int32" } ], "responses": { "200": { "description": "OK", "schema": { "type": "array", "items": { "$ref": "#/definitions/Demo" } } }, "401": { "description": "Unauthorized" }, "403": { "description": "Forbidden" }, "404": { "description": "Not Found" } } } } }, "definitions": { "Demo": { "type": "object", "properties": { "age": { "type": "string" }, "id": { "type": "string" }, "name": { "type": "string" } } } }}
API详细说明:
作用范围 API 使用位置
@ApiImplicitParam
属性 取值 作用
以上就是使用Swagger搭建RESTful风格测试Mock Server,生成Springboot代码的全部过程,整个开发过程其实很简单,但网络上的资料太少,经过不断的踩坑,终于调试成功。
阅读全文
1 0
- 使用Swagger生成JAVA Mock Server(Springboot)代码
- springboot 整合generator代码生成工具 使用swagger展示接口 使用pagehelper做分页
- swagger-codegen生成java客户端代码(其他语言类似)
- Swagger搭配Springboot使用
- SpringBoot中使用Swagger生成RESTful规范API文档
- Spring MVC中使用Swagger生成API文档和完整项目示例Demo,swagger-server-api
- Spring MVC中使用Swagger生成API文档和完整项目示例Demo,swagger-server-api
- Spring MVC中使用Swagger生成API文档和完整项目示例Demo,swagger-server-api
- Spring MVC中使用Swagger生成API文档和完整项目示例Demo,swagger-server-api
- Spring MVC中使用Swagger生成API文档和完整项目示例Demo,swagger-server-api
- Spring MVC中使用Swagger生成API文档和完整项目示例Demo,swagger-server-api
- Spring MVC中使用Swagger生成API文档和完整项目示例Demo,swagger-server-api
- Swagger集成Springboot生成Restful api
- SpringBoot整合Swagger自动生成API文档
- springboot 集成 swagger生成在线文档
- API生命周期第三阶段:API实施:使用swagger codegen生成可部署工程,择取一个作为mock service
- springboot+mockito (二)mock
- springboot + swagger
- 全局变量
- 贝叶斯估计
- 【C#】程序结构
- 笔记10
- Linux三个网络监视器之《一》——iftop
- 使用Swagger生成JAVA Mock Server(Springboot)代码
- 标准C库封装的样例
- P4080 RCW总结
- Java中的自动装箱与拆箱
- 缓存事件轮询表
- AIDL的使用
- oracle中这是什么用法: connect by prior start with
- 身份认证
- VScode入门技巧