整合swagger2生成Restful Api接口文档

swagger Restful文档生成工具 2017-9-30

官方地址:https://swagger.io/docs/specification/about/

官方Github:https://github.com/swagger-api/swagger-core/wiki/Annotations

启动项目,访问http://localhost:8082/swagger-ui.html查看API

注意,此项目示例中,使用了三种ui依赖,每种依赖对应的访问页面不同:

springfox-swagger-ui -> http://localhost:8082/swagger-ui.html
swagger-bootstrap-ui -> http://localhost:8082/doc.html
swagger-ui-layer -> http://localhost:8082/docs.html

使用方法:

1.添加依赖(springfox-swagger2依赖是必须的,三种ui依赖只需要使用一个就行)

<dependency><groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.2.2</version> </dependency>

2.创建配置文件Swagger2Config.java

@EnableSwagger2
@Configuration
public class Swagger2Config { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() //为当前包路径 .apis(RequestHandlerSelectors.basePackage("com.zyd.controller")) .paths(PathSelectors.any()) .build(); } //构建 api文档的详细信息函数 private ApiInfo apiInfo() { return new ApiInfoBuilder() //页面标题 .title("Spring Boot 测试使用 Swagger2 构建RESTful API") .termsOfServiceUrl("http://localhost/") //创建人 .contact("zhyd") //版本号 .version("1.0") //描述 .description("API 描述") .build(); } }

注:@EnableSwagger2注解一定不要漏掉

3.编写文档

@RestController
@RequestMapping("/demo") @Api(value = "测试Swagger2",description="简单的API") public class UserController { @ApiOperation(value = "创建用户", notes = "根据User对象创建用户") @ApiImplicitParams({ @ApiImplicitParam(dataType = "java.lang.Long", name = "id", value = "id", required = true, paramType = "path"), @ApiImplicitParam(dataType = "User", name = "user", value = "用户信息", required = true) }) @ApiResponses({ @ApiResponse(code = 500, message = "接口异常"), }) @RequestMapping(value = "/user/{id}", method = RequestMethod.POST) public User insert(@PathVariable Long id, @RequestBody User user) { System.out.println("id:" + id + ", user:" + user); user.setId(id); return user; } }

注意:如果api文档只是针对开发人员使用的,就需要后台对v2/api-docs路径进行过滤,对非开发人员应该是不可见的。

自定义api页面

本例是使用的swagger-ui-layer主题(链接请见本文最后)。使用自定义api页面就不需要在pom中配置ui依赖了,详情查看static目录

api页面访问地址:http://localhost:8082/api.html

页面效果参考

swagger-ui.html

bootstrap-ui.html

layer-ui.html.html

layer-ui-custom.html

参考链接

swagger-ui-layer地址:https://github.com/caspar-chen/swagger-ui-layer

Swagger-Bootstrap-UI地址:https://github.com/xiaoymin/Swagger-Bootstrap-UI

有问题欢迎留言(可能回复有延迟,见谅)。

其他

源码请移步:Github源码

相关文章导读

  1. SpringBoot项目实战(8):四种读取properties文件的方式
  2. SpringBoot项目实战(7):自定义异常处理界面
  3. SpringBoot项目实战(6):开启定时任务
  4. SpringBoot项目实战(5):集成分页插件
  5. SpringBoot项目实战(4):集成Mybatis
  6. SpringBoot项目实战(3):整合Freemark模板
  7. SpringBoot项目实战(2):集成SpringBoot
  8. SpringBoot项目实战(1):新建Maven项目

https://www.imooc.com/article/20521

webapi文档描述-swagger

https://www.codercto.com/a/23839.html

http://blog.didispace.com/spring-boot-starter-swagger-1.2.0/

慕课手记:

http://www.imooc.com/article/15384

转载于:https://www.cnblogs.com/gaogaoyanjiu/p/9662018.html

整合swagger2生成Restful Api接口文档相关推荐

  1. swagger php 生成api,blog/Swagger生成php restful API接口文档.md at master · lfq618/blog · GitHub...

    Swagger生成php restful API接口文档 背景 我们的restful api项目采用yaf框架, 整体结构简单, 我们只需要用swagger扫描 application目录即可. 下面 ...

  2. Swagger 生成 PHP API 接口文档

    Swagger 生成 PHP API 接口文档 标签(空格分隔): php 1.概况 有同学反馈写几十个接口文档需要两天的工作量, 随着多部门之间的协作越来越频繁, 维护成本越来越高, 文档的可维护性 ...

  3. Swagger 生成 PHP restful API 接口文档

    需求和背景 需求: 为客户端同事写接口文档的各位后端同学,已经在各种场合回忆了使用自动化文档工具前手写文档的血泪史. 我的故事却又不同,因为首先来说,我在公司是 Android 组负责人,属于上述血泪 ...

  4. RESTful API接口文档规范小坑

    希望给你3-5分钟的碎片化学习,可能是坐地铁.等公交,积少成多,水滴石穿,谢谢关注. 前后端分离的开发模式,假如使用的是基于RESTful API的七层通讯协议,在联调的时候,如何避免配合过程中出现问 ...

  5. Spring Boot入门(16):Spring Boot 整合 Swagger-UI 实现在线API接口文档 | 超级详细,建议收藏

    1. 前言

  6. 芋道 Spring Boot API 接口文档 Swagger 入门

    点击上方"芋道源码",选择"设为星标" 做积极的人,而不是积极废人! 源码精品专栏 原创 | Java 2020 超神之路,很肝~ 中文详细注释的开源项目 RP ...

  7. Spring Boot集成Restful Api在线文档接口调试工具 Swagger

    文章目录 一.Swagger简介 Swagger有什么用? 二.环境准备 三.构建Spring Boot工程 四.引入Swagger依赖 五.编写一个Test控制器 六.配置Swagger 七.最终测 ...

  8. php怎么根据接口文档实现功能,CodeIgniter+swagger实现 PHP API接口文档自动生成功能...

    一.安装swagger 1.首先需要有composer,没有的自行百度安装 2.下载swagger,打开网站https://packagist.org/packages/zircote/swagger ...

  9. 开发日记-20190328 关键词 利用eolinker一键快速生成API接口文档

    今天感觉效率真的很低= =各个层面的,apk发布到现场发现出现了问题,所以一个下午都在忙着解决现场出现的问题,领导一直打电话询问进度,午觉也没有睡所以今天预计的很多计划都处于停滞状态,像昨天规划的今天 ...

最新文章

  1. 2022-2028年中国顺丁橡胶行业发展模式分析及市场分析预测报告
  2. 线程的创建与启动——Thread 类有两个常用的构造方法:Thread()与 Thread(Runnable)||多线程运行结果是随机的
  3. Git与GitHub的使用
  4. 一次Linux磁盘损坏导致系统不可用恢复实例
  5. mysql innodb索引原理
  6. OJ1080: a+b(多实例测试3)(C语言)
  7. docker mysql8.0挂载_Docker安装MySQL 8.0.17 并挂载数据及配置文件,修改时区
  8. matlab2c使用c++实现matlab函数系列教程-save函数
  9. php mysql 子查询_php – 如何在mySQL的子查询中指定父查询字段?
  10. 用DISM修复Win10系统文件教程
  11. 获取网站CDN加速的真实服务器IP方法
  12. oracle季度日均怎么算,求日均值,该如何处理
  13. 「AI Timer 说」我只是没有行动而已,我笃定。
  14. 『这辈子就相爱《何苦要等下辈子》 李草青青、肖玄MV』
  15. [4G5G专题-129]:RF-架构演进的驱动力与RF常见术语
  16. linux bootrom ftp,H3C交换机通过以太口应用ftp方式升级bootrom软件
  17. 聚光灯下的熊猫TV技术架构演进
  18. win10中搭建并配置ftp服务器的方法(实现多用户登录整合版)
  19. 泰坦尼克号python数据预处理_sklearn preprocessing 数据预处理(OneHotEncoder)
  20. 演讲实录丨戴琼海院士《人工智能:算法·算力·交互》

热门文章

  1. JAVA中indexOf函数用法
  2. Jenkins 管理界面里提示“反向代理设置有误“的问题解决办法
  3. 调试笔记--keil 断点调试小技巧
  4. CTFshow 反序列化 web277
  5. 如何避免GUIDE自动代码的Warning
  6. 常用的图像增强处理办法
  7. shell `-c`参数 如何使用
  8. img.item()跟img[x,y]
  9. 感知机模型[神经网络入门]
  10. PHP查找数据库中的用户,php-在数据库中查找现有的电子邮件和用户...