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:业务逻辑层实现及 ...
随机推荐
- 18.实现如下类之间的继承关系,并编写Music类来测试这些类。
package zhongqiuzuoye; public class Instrument { public void play() { System.out.println("弹奏乐器& ...
- Windows Server 2012部署Enterprise Solution 5.4
最近一个客户升级系统,改用Windows Server 2012作为服务器操作系统.升级之后性能未见明显的改善,在不改变代码的基础上,考虑到C/S架构的能力,增加内存或是处理器的处理能力似乎可以解决一 ...
- CSS中的::after ::before
利用::after和before来清除浮动 <!DOCTYPE html> <html lang="en"> <head> <meta c ...
- 编写Shader时的一些性能考虑
编写shader时的一些建议:1.只计算需要计算的东西:2.通常,需要渲染的像素比顶点数多,而顶点数又比物体数多很多.所以如果可以,尽量将运算从PS移到VS,或直接通过script来设置某些固定值:3 ...
- PHP的学习--解析URL
PHP中有两个方法可以用来解析URL,分别是parse_url和parse_str. parse_url 解析 URL,返回其组成部分 mixed parse_url ( string $url [, ...
- Network - SSH
SSH(Secure Shell) https://wiki.wireshark.org/SSH SSH 协议与OpenSSH详解 http://my.oschina.net/liting/blo ...
- Web 前端开发精华文章集锦(jQuery、HTML5、CSS3)【系列十八】
<Web 前端开发精华文章推荐>2013年第六期(总第十八期)和大家见面了.梦想天空博客关注 前端开发 技术,分享各种增强网站用户体验的 jQuery 插件,展示前沿的 HTML5 和 C ...
- Azure ARM (11) ARM模式下,创建虚拟机并配置负载均衡器
<Windows Azure Platform 系列文章目录> 本文内容比较多,请大家仔细阅读,谢谢! 在前几章中,我们做了准备工作: 1.创建ARM Resouce Group,叫Lei ...
- 如何使用Jedis操作Redis消息队列
资源链接 Jedis的jar包 Commons-io的jar包 使用方法 代码样例如下,使用前,注意打开redis的server程序. 代码样例 package RedisExample; impor ...
- IIS各个版本中你需要知道的那些事儿
一.写在前面 目前市面上所用的IIS版本估计都是>=6.0的.所以我们主要以下面三个版本进行讲解 服务器版本 IIS默认版本 server2003 6.0 server2008 7.0 serv ...