Swagger接入
一 简介
Swagger是一个开源项目,用于描述和生成RestAPi的文档。可以帮助开发人员快速了解web服务的功能。
二 接入Swagger流程
1.在所在Module的pom.xml中,添加Swagger annotation 依赖
<dependency> <groupId>com.wordnik</groupId> <artifactId>swagger-annotations</artifactId> <scope>compile</scope> <version>1.5.1-M2</version></dependency> |
2.在classpath:/templates/目录,添加swagger maven plugin需要的模板templates.zip

3.在Controller所在Module的pom.xml文件中配置swagger maven plugin
<plugin> <groupId>com.swagger.test</groupId> <artifactId>swagger-maven-plugin</artifactId> <version>3.0.5</version> <configuration> <enable>${doc-enable}</enable> <!--注意这里 --> <apiSources> <apiSource> <springmvc>true</springmvc> <!--是否spring mvc项目,我们的项目都填是 --> <locations>com.swagger.web</locations><!--Controller所在包 --> <schemes>http</schemes><!--协议 --> <host></host><!-- 服务器地址,空值表示跟文档是同一台机器(域名) --> <basePath>/test</basePath><!-- api url 前缀 --> <info> <title>test API</title><!-- Api 标题啊 --> <version>v1</version><!-- Api版本 --> <description>test API的详细描述</description><!-- Api的详细描述 --> <termsOfService> http://www.test.com </termsOfService> <contact> <email>123@qq.com</email><!-- 联系人邮件 --> <name>yourname</name><!-- 联系人姓名 --> </contact> </info> <templatePath>classpath:/templates/strapdown.html.hbs</templatePath><!-- 文档模板 --> <outputPath>${basedir}/src/main/webapp/doc/document.html</outputPath><!-- 文档输出路径,设置在和SwaggerUI同一目录 --> <swaggerDirectory>${basedir}/src/main/webapp/doc/</swaggerDirectory><!-- Swagger.json输出路径,设置在和SwaggerUI同一目录 --> </apiSource> </apiSources> </configuration> <executions> <execution> <phase>compile</phase> <goals> <goal>generate</goal> </goals> </execution> </executions></plugin> |
其中,enable如果设置为true的话,就会一直生成文档,如果想分环境设置的话,可以在profile里配置doc-enable变量属性实现
<profiles> <profile> <id>local</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <conf-dir>local</conf-dir> <doc-enable>true</doc-enable> </properties> </profile></profiles> |
4.将Swagger UI添加到项目的web目录下swagger_ui.zip,修改index.html文件第29行
url = "/jingquadmin/doc/swagger.json" TO url = "/yoururl/doc/swagger.json"
(另,附件中的document.html和swagger.json是插件swagger maven plugin自动生成的,添加与否均可)

(添加Swagger UI,主要是为了API的可视化)
5.在Controller上添加API annotation,在Controller接口上添加Swagger annotation

6.maven编译项目,然后启动项目或plus发布项目,链接如下:
http://host/basepath/doc/index.html 自动测试平台
http://host/basepath/doc/document.html API文档
三 Swagger常用注解介绍
|
|
|
|
|---|---|---|
|
annotation
|
说明
|
属性介绍
|
| Api | 加在Controller类上,将Controller标记为Swagger资源 |
value - 名称 description - 描述 |
| ApiOperation | 加在Controller方法上,表明这个方法是一个rest接口,一个@Api下可有多个@ApiOperation(一个Controller文件可有多个接口) |
value - 接口名称 notes - 详细描述 response - 返回类 |
| ApiImplicitParams/ApiImplicitParam/ApiParam | 加在Controller方法上,表明这里需要隐藏的参数 |
name - 参数名 value - 参数描述 defaultValue - 默认值 required - 是否必需 dataType - 数据类型 |
| ApiModel | 加在Model类上,用来描述封装的参数对象和返回的参数对象 | description - 描述 |
| ApiModelProperty | 加在Model类的属性上,描述参数的对象属性 |
value - 属性描述 |
完整Annotation详见http://docs.swagger.io/swagger-core/current/apidocs/index.html?io/swagger/annotations/Api.html
其他:
SpringMVC项目也可以通过接入Springfox来自动生成API文档,Springfox的前身是swagger-SpringMVC,一个开源的API doc框架。
注意:
在Controller文件中定义接口时,@RequestMapping注解中的method属性是必须要设置的,否则Swagger插件不会生成对应接口信息;
@RequestMapping(value = "/v2/group", method = RequestMethod.GET)
*内部人员(绿山墙注)
Swagger接入的更多相关文章
- ASP.NET Core Swagger接入使用IdentityServer4 的 WebApi
写在前面 是这样的,我们现在接口使用了Ocelot做网关,Ocelot里面集成了基于IdentityServer4开发的授权中心用于对Api资源的保护.问题来了,我们的Api用了SwaggerUI做接 ...
- 接口文档神器Swagger(下篇)
本文来自网易云社区 作者:李哲 二.Swagger-springmvc原理解析 上面介绍了如何将springmvc和springboot与swagger结合,通过简单配置生成接口文档,以及介绍了swa ...
- Asp.Net Core SwaggerUI 接入
Asp.Net Core SwaggerUI 接入 简单了解 swagger的目的简单来说就是,不用为每个接口手动写接口文档,因为它是根据接口自动生成的,接口更改时文档也同步更新,减少了手动更新的麻烦 ...
- swagger在线api文档搭建指南,用于线上合适么?
在上一篇文章中,我们讲解了什么是 api,什么是 sdk: https://www.cnblogs.com/tanshaoshenghao/p/16217608.html 今天将来到我们万丈高楼平地起 ...
- [Abp vNext 入坑分享] - 6.完整接入swagger
前言 由于最近一直在修改一下排版,同时找了非技术的朋友帮忙看一下排版的问题,现在已经基本上确定了排版和样式了.更新可以恢复正常了. 作为一个写前端代码基本只写js不写css的开发,搞排版真的头疼..各 ...
- Asp.Net Core WebApi中接入Swagger组件(初级)
开发WebApi时通常需要为调用我们Api的客户端提供说明文档.Swagger便是为此而存在的,能够提供在线调用.调试的功能和API文档界面. 环境介绍:Asp.Net Core WebApi + S ...
- WebApi接入Swagger
1.新建webApi项目 2.nuget引入 swagger 3.在项目属性里配置输出 xml文件 4.打开SwaggerConfig.cs编辑 protected static string Get ...
- spring接入swagger后单元测试报错
2017-05-16 11:58:05.276 INFO 11268 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing ...
- net core体系-API-Restful+Swagger搭建API
本篇主要简单介绍下.net core下搭建WebApi 项目结构 项目结构其实不用多说,基本上大同小异. Controller:对外暴露的契约 Business/IBussiness:业务逻辑层实现及 ...
随机推荐
- 安装Jenkins
jenkins是一个广泛用于持续构建的可视化web工具,持续构建说得更直白点,就是各种项目的"自动化"编译.打包.分发部署的工具 1.jenkins的官网 2.jenkins各个系 ...
- 每天一个linux命令(29):chgrp命令
在lunix系统里,文件或目录的权限的掌控以拥有者及所诉群组来管理.可以使用chgrp指令取变更文件与目录所属群组,这种方式采用群组名称或群组识别码都可以.Chgrp命令就是change group的 ...
- Python魔法 - MetaClass
Python魔法 - MetaClass metaclass The class of a class. Class definitions create a class name, a class ...
- SpringBoot常用配置简介
SpringBoot常用配置简介 1. SpringBoot中几个常用的配置的简单介绍 一个简单的Spring.factories # Bootstrap components org.springf ...
- CCNA学习 NAT网络地址转换
CCNA基础 NAT网络地址转换 在计算机网络中,网络地址转换(Network Address Translation,缩写为NAT),也叫做网络掩蔽或者IP掩蔽(IP masquerading),是 ...
- MongoDB修改器总结
1"$set":用来制定一个字段值,若不存在,则创建: 一般用于点加一个字段 db.users.update({name:"joe"},{" ...
- GridView里做页面的链接
采用Gridview的OnRowDataBound属性 后台 protected void gvTranslateInfo_RowDataBound(object sender, GridViewRo ...
- Hadoop官方文档翻译——YARN Architecture(2.7.3)
The fundamental idea of YARN is to split up the functionalities of resource management and job sched ...
- 必应词典3.2去广告备忘笔记(转摘于roustar31)
下载安装包后,沙盘运行,得到本体BingDict_Setup.msi文件,起作用的就是这个,其他的无视了.使用命令行参数:msiexec /a "d:\BingDict_Setup.msi& ...
- CSS 魔法系列:纯 CSS 绘制图形(各种形状的钻石)
我们的网页因为 CSS 而呈现千变万化的风格.这一看似简单的样式语言在使用中非常灵活,只要你发挥创意就能实现很多比人想象不到的效果.特别是随着 CSS3 的广泛使用,更多新奇的 CSS 作品涌现出来. ...