一、遇到的问题

作为一名coder,经常需要向别人提供接口,或者调用别人的接口。于是就有了接口参数是什么意思,要怎么传参数,返回值是什么意思……有多少调用方,就会有多少人来询问这些参数。如果是长时间之后,自己或许都不知道这些参数是什么意思。于是维护接口文档便成了一项必不可少的工作,维护文档也有很多问题:

  • 如果手工写会很费劲
  • 接口变更后可能会忘了同步文档
  • ……

二、Swagger配置

Swagger(官方文档)可以快速帮助实现接口api的维护与简单测试。

a、引入maven依赖包

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

b、配置Swagger

@Configuration
@EnableSwagger2
@Profile("dev")
public class SwaggerConfig {
public static final String SWAGGER_SCAN_BASE_PACKAGE = "api.doc.demo.controller";
public static final String VERSION = "1.0.0"; @Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
/**api接口包扫描路径*/
.apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_BASE_PACKAGE))
/**可以根据url路径设置哪些请求加入文档,忽略哪些请求*/
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
/**设置文档的标题*/
.title("Swagger2 接口文档示例")
/**设置文档的描述*/
.description("更多内容请关注:http://www.abc.com")
/**设置文档的联系方式*/
.contact(new Contact("create by XXX", "http://www.abc.com", "xxxx.@xxx.com"))
/**设置文档的License信息->1.3 License information*/
.termsOfServiceUrl("www.abc.com")
.license("xxx")
.licenseUrl("http://www.xxx.com")
/**设置文档的版本信息-> 1.1 Version information*/
.version(VERSION)
.build();
}
}

c、常用注解

  • @ApiOperation : api说明
    @ApiOperation(value="获取用户列表", notes="获取所有用户列表",produces = "application/json")
@RequestMapping(value="/getList", method= RequestMethod.GET)
public List<UserModel> getUserList() {
return getDemoList();
}

  • @ApiResponses :默认Response的基础上增加新的Response说明
  • @ApiImplicitParam : 描述接口参数
@ApiImplicitParam(name = "userId",value = "用户ID",dataType = "int",paramType = "path")
  • @ApiImplicitParams : 多个参数
@ApiImplicitParams({
@ApiImplicitParam(name = "id",value = "用户ID",paramType = "path",dataType = "int"),
@ApiImplicitParam(name = "userName",value = "用户名称",paramType = "form",dataType = "string")
})
  • @ApiModel : model属性
@ApiModel(value = "user", description = "user对象")
public class UserModel {
@ApiModelProperty(value = "ID", dataType = "Integer", required = true)
private Integer userId;
@ApiModelProperty(value = "用戶名", dataType = "String")
private String userName;
@ApiModelProperty(value = "性別", dataType = "Integer", allowableValues = "0,1,2")
private Integer sex;
}

这样就可以通过访问 http://localhost:8080/swagger-ui.html 看到接口API调用说明。demo

【工具】Swagger2写接口注释的更多相关文章

  1. Java的LockSupport工具,Condition接口和ConditionObject

    在之前我们文章(关于多线程编程基础和同步器),我们就接触到了LockSupport工具和Condition接口,之前使用LockSupport工具来唤醒阻塞的线程,使用Condition接口来实现线程 ...

  2. Spring Boot (十五): 优雅的使用 API 文档工具 Swagger2

    1. 引言 各位在开发的过程中肯定遇到过被接口文档折磨的经历,由于 RESTful 接口的轻量化以及低耦合性,我们在修改接口后文档更新不及时,导致接口的调用方(无论是前端还是后端)经常抱怨接口与文档不 ...

  3. 在Eclipse中怎样写Java注释

    java中的注释分为实现注释和文档注释 实现注释就是那些/……../和//……的注释,是注释程序用的,文档注释是/*……./的注释,是用来生成javadoc的.设置方法如下: 1.打开Eclipse的 ...

  4. 【百度地图API】如何在地图上添加标注?——另有:坐标拾取工具+打车费用接口介绍

    原文:[百度地图API]如何在地图上添加标注?--另有:坐标拾取工具+打车费用接口介绍 摘要: 在这篇文章中,你将学会,如何利用百度地图API进行标注.如何使用API新增的打车费用接口. ------ ...

  5. 前端必备之Node+mysql+ejs模版如何写接口

    前端必备之Node+mysql+ejs模版如何写接口 这星期公司要做一个视频的后台管理系统, 让我用Node+mysql+ejs配合写接口, 周末在家研究了一下, 趁还没来具体需求把研究内容在这里分享 ...

  6. node+pm2+express+mysql+sequelize来搭建网站和写接口

    前面的话:在这里已经提到了安装node的方法,node是自带npm的.我在技术中会用es6去编写,然后下面会分别介绍node.pm2.express.mysql.sequelize.有少部分是摘抄大佬 ...

  7. Springboot集成swagger2生成接口文档

    [转载请注明]: 原文出处:https://www.cnblogs.com/jstarseven/p/11509884.html    作者:jstarseven    码字挺辛苦的.....   一 ...

  8. Java后端开发工作 - 写接口

    我在公司的工作内容是,对于一个BS应用,负责服务器端开发工作,Java语言.与前端开发人员合作,最终提供给前端RESTFUL接口,保证页面正常响应. 经验之谈 一个接口可以理解为一个业务逻辑,一个业务 ...

  9. 「快学springboot」16.让swagger帮忙写接口文档

    swagger简介 官方的介绍 THE WORLD'S MOST POPULAR API TOOLING Swagger is the world's largest framework of API ...

随机推荐

  1. python 绘制3D散点图

    import scipy.io as sio from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt a = [ ...

  2. Python 打包程序

    一.打包成exe 1.安装pyinstaller #只要你能FQ连接https://pypi.python.org/pypi下载会很快,不用担心超时问题. https://pypi.python.or ...

  3. iOS UILable 高度自适

    #import "ViewController.h" #define FontSize 20 @interface ViewController () @end @implemen ...

  4. zookeeper两台内网服务器彼此调不到服务的问题。

    Start NettyClient /172.20.11.52 connect to the server /172.20.11.52:20881, dubbo version: 2.5.3, cur ...

  5. LeetCode 笔记系列十 Suduko

    题目:Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by ...

  6. http协议----->http请求方式,post,get

    4.http请求方式有七种(http请求是想web资源请求数据) Post get head options delete trace put 常用:GET POST POST例如form表单提交,G ...

  7. Python爬虫实例(一)爬取百度贴吧帖子中的图片

    程序功能说明:爬取百度贴吧帖子中的图片,用户输入贴吧名称和要爬取的起始和终止页数即可进行爬取. 思路分析: 一.指定贴吧url的获取 例如我们进入秦时明月吧,提取并分析其有效url如下 http:// ...

  8. print文档

    文档 def print(self, *args, sep=' ', end='\n', file=None): # known special case of print ""& ...

  9. 如何保护自己的GitHub代码不被别人覆盖

    我们在自己的github上创建了免费的公开代码,为了防止别人通过git push upstream master 覆盖了自己原有的代码,需要作一下设置:Settings->Branchs,然后在 ...

  10. css3 利用dispaly:flex

    直接上代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...