springboot利用swagger构建api文档
来源:互联网 发布:酒店2000w数据 编辑:程序博客网 时间:2024/06/15 14:29
springboot利用swagger构建api文档
前言
Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件。本文简单介绍了在项目中集成swagger的方法。
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步。Swagger 让部署管理和使用功能强大的API从未如此简单。
使用
1.添加swagger依赖
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.6.1</version></dependency><dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.6.1</version></dependency>
2.添加swagger配置
@Configuration@EnableSwagger2public class Swagger2 { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.ybf.activity.web.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("springboot利用swagger构建api文档") .description("简单优雅的restful风格,http://luckystar88.github.io/") .termsOfServiceUrl("http://luckystar88.github.io/") .version("1.0") .build(); }}
注意spring boot的包结构,否则会导致配置无效。示例程序的包结构:
com.ybf.activity.web|
———————-|config
—————————–Swagger2.java
———————-Application.java
3.在接口方法上使用注解添加描述信息
@ApiOperation(value = "用户某个用户的信息",notes = "根据用户id获取用户详细信息")@ApiImplicitParam(name = "id",value = "用户id",required = true,dataType = "int",paramType = "path")@RequestMapping(value = "/student/{id}",method = RequestMethod.GET)@ResponseBodypublic Student student(@PathVariable int id) { return studentMapper.getById(id);}@ApiOperation(value = "获取用户信息(分页)",notes = "根据传入的页数获取用户信息")@ApiImplicitParam(name="pageNo",value = "页数",required = true,dataType = "int",paramType = "path")@RequestMapping(value = "/student/page/{pageNo}",method = RequestMethod.GET)@ResponseBodypublic List<Student> selectStudentByPage(@PathVariable int pageNo) { if (pageNo > 0) { PageHelper.startPage(pageNo,3); // 设置分页,参数1=页数,参数2=每页显示条数 } return studentMapper.sel();}
默认会对所有的方法生成API文档,如果某个方法不需要,可以添加@ApiIgnore。
启动SpringBoot程序,浏览器输入:项目上下文路径/swagger-ui.html就可以看到效果。
点击某个请求,可以查看详情:
输入参数,点击Try it out按钮可以查看响应。
阅读全文
0 0
- springboot利用swagger构建api文档
- SpringBoot&Swagger构建REST API并生成API文档
- SpringBoot + mybatis + Swagger快速构建REST API并生成优美的API文档
- SpringBoot项目API文档工具-Springfox Swagger
- SpringBoot整合Swagger自动生成API文档
- jersey+swagger,构建api文档平台
- SpringBoot中使用Swagger生成RESTful规范API文档
- swagger-ui教程-构建api接口文档工具
- swagger-ui教程-构建api接口文档工具
- swagger-ui教程 构建api接口文档工具
- Swagger生成API文档
- 利用Swagger Maven Plugin生成Rest API文档
- 使用Swagger在SpringBoot项目中管理API文档(使用Oauth2)
- 使用Spring Boot&Swagger快速构建REST API并生成优美的API文档
- eclipse 使用maven构建 springboot +swagger
- SpringBoot开发详解(八) -- 使用Swagger2构建API文档
- Springboot中使用Swagger2构建RESTful API文档
- springboot 集成 swagger生成在线文档
- CodeForces
- 结构,联合和枚举
- 如何获取type=radio属性的value值
- java 反射机制的几个调用方法
- PDF如何转换成HTML
- springboot利用swagger构建api文档
- BZOJ 3052: [wc2013]糖果公园 树上莫队
- js开源插件速查表
- git新建本地仓库并上传到云端
- windows 系统常用 dll
- Minimum Index Sum of Two Lists问题及解法
- linux 学习20170726 查看进程号及环境变量、获取字符串长度及shell版本
- 2017年多校赛第一场 1006 Function(枚举)
- 完全分布式安装Hadoop,Hive,Hbase,Hwi,Zookeeper