Swagger UI使用指南
1:认识Swagger
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步。
作用:
1. 接口的文档在线自动生成。
2. 功能测试。
Swagger是一组开源项目,其中主要要项目如下:
1. Swagger-tools:提供各种与Swagger进行集成和交互的工具。例如模式检验、Swagger 1.2文档转换成Swagger 2.0文档等功能。
2. Swagger-core: 用于Java/Scala的的Swagger实现。与JAX-RS(Jersey、Resteasy、CXF...)、Servlets和Play框架进行集成。
3. Swagger-js: 用于JavaScript的Swagger实现。
4. Swagger-node-express: Swagger模块,用于node.js的Express web应用框架。
5. Swagger-ui:一个无依赖的HTML、JS和CSS集合,可以为Swagger兼容API动态生成优雅文档。
6. Swagger-codegen:一个模板驱动引擎,通过分析用户Swagger资源声明以各种语言生成客户端代码。
2:Maven
版本号请根据实际情况自行更改。
<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>
3:创建Swagger2配置类
在Application.java同级创建Swagger2的配置类Swagger2
- package com.swaggerTest;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import springfox.documentation.builders.ApiInfoBuilder;
- import springfox.documentation.builders.PathSelectors;
- import springfox.documentation.builders.RequestHandlerSelectors;
- import springfox.documentation.service.ApiInfo;
- import springfox.documentation.spi.DocumentationType;
- import springfox.documentation.spring.web.plugins.Docket;
- import springfox.documentation.swagger2.annotations.EnableSwagger2;
- /**
- * Swagger2配置类
- * 在与spring boot集成时,放在与Application.java同级的目录下。
- * 通过@Configuration注解,让Spring来加载该类配置。
- * 再通过@EnableSwagger2注解来启用Swagger2。
- */
- @Configuration
- @EnableSwagger2
- public class Swagger2 {
- /**
- * 创建API应用
- * apiInfo() 增加API相关信息
- * 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现,
- * 本例采用指定扫描的包路径来定义指定要建立API的目录。
- *
- * @return
- */
- @Bean
- public Docket createRestApi() {
- return new Docket(DocumentationType.SWAGGER_2)
- .apiInfo(apiInfo())
- .select()
- .apis(RequestHandlerSelectors.basePackage("com.swaggerTest.controller"))
- .paths(PathSelectors.any())
- .build();
- }
- /**
- * 创建该API的基本信息(这些基本信息会展现在文档页面中)
- * 访问地址:http://项目实际地址/swagger-ui.html
- * @return
- */
- private ApiInfo apiInfo() {
- return new ApiInfoBuilder()
- .title("Spring Boot中使用Swagger2构建RESTful APIs")
- .description("更多请关注http://www.baidu.com")
- .termsOfServiceUrl("http://www.baidu.com")
- .contact("sunf")
- .version("1.0")
- .build();
- }
- }
如上代码所示,通过createRestApi函数创建Docket的Bean之后,apiInfo()用来创建该Api的基本信息(这些基本信息会展现在文档页面中)。
4:添加文档内容
在完成了上述配置后,其实已经可以生产文档内容,但是这样的文档主要针对请求本身,描述的主要来源是函数的命名,对用户并不友好,我们通常需要自己增加一些说明来丰富文档内容。
Swagger使用的注解及其说明:
@Api:用在类上,说明该类的作用。
@ApiOperation:注解来给API增加方法说明。
@ApiImplicitParams : 用在方法上包含一组参数说明。
@ApiImplicitParam:用来注解来给方法入参增加说明。
@ApiResponses:用于表示一组响应
@ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息
l code:数字,例如400
l message:信息,例如"请求参数没填好"
l response:抛出异常的类
@ApiModel:描述一个Model的信息(一般用在请求参数无法使用@ApiImplicitParam注解进行描述的时候)
l @ApiModelProperty:描述一个model的属性
注意:@ApiImplicitParam的参数说明:
|
paramType:指定参数放在哪个地方 |
header:请求参数放置于Request Header,使用@RequestHeader获取 query:请求参数放置于请求地址,使用@RequestParam获取 path:(用于restful接口)-->请求参数的获取:@PathVariable body:(不常用) form(不常用) |
|
name:参数名 |
|
|
dataType:参数类型 |
|
|
required:参数是否必须传 |
true | false |
|
value:说明参数的意思 |
|
|
defaultValue:参数的默认值 |
例子:
- package com.swaggerTest.controller;
- import org.springframework.stereotype.Controller;
- import org.springframework.util.StringUtils;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.ResponseBody;
- import io.swagger.annotations.Api;
- import io.swagger.annotations.ApiImplicitParam;
- import io.swagger.annotations.ApiImplicitParams;
- import io.swagger.annotations.ApiOperation;
- /**
- * 一个用来测试swagger注解的控制器
- * 注意@ApiImplicitParam的使用会影响程序运行,如果使用不当可能造成控制器收不到消息
- *
- * @author SUNF
- */
- @Controller
- @RequestMapping("/say")
- @Api(value = "SayController|一个用来测试swagger注解的控制器")
- public class SayController {
- @ResponseBody
- @RequestMapping(value ="/getUserName", method= RequestMethod.GET)
- @ApiOperation(value="根据用户编号获取用户姓名", notes="test: 仅1和2有正确返回")
- @ApiImplicitParam(paramType="query", name = "userNumber", value = "用户编号", required = true, dataType = "Integer")
- public String getUserName(@RequestParam Integer userNumber){
- if(userNumber == 1){
- return "张三丰";
- }
- else if(userNumber == 2){
- return "慕容复";
- }
- else{
- return "未知";
- }
- }
- @ResponseBody
- @RequestMapping("/updatePassword")
- @ApiOperation(value="修改用户密码", notes="根据用户id修改密码")
- @ApiImplicitParams({
- @ApiImplicitParam(paramType="query", name = "userId", value = "用户ID", required = true, dataType = "Integer"),
- @ApiImplicitParam(paramType="query", name = "password", value = "旧密码", required = true, dataType = "String"),
- @ApiImplicitParam(paramType="query", name = "newPassword", value = "新密码", required = true, dataType = "String")
- })
- public String updatePassword(@RequestParam(value="userId") Integer userId, @RequestParam(value="password") String password,
- @RequestParam(value="newPassword") String newPassword){
- if(userId <= 0 || userId > 2){
- return "未知的用户";
- }
- if(StringUtils.isEmpty(password) || StringUtils.isEmpty(newPassword)){
- return "密码不能为空";
- }
- if(password.equals(newPassword)){
- return "新旧密码不能相同";
- }
- return "密码修改成功!";
- }
- }
完成上述代码添加上,启动Spring Boot程序,访问:http://localhost:8080/swagger-ui.html
如上图,可以看到暴漏出来的控制器信息,点击进入可以看到详细信息。
两个注意点:
1. paramType会直接影响程序的运行期,如果paramType与方法参数获取使用的注解不一致,会直接影响到参数的接收。
例如:
使用Sawgger UI进行测试,接收不到!
2. 还有一个需要注意的地方:
Conntroller中定义的方法必须在@RequestMapper中显示的指定RequestMethod类型,否则SawggerUi会默认为全类型皆可访问, API列表中会生成多条项目。
如上图:updatePassword()未指定requestMethod,结果生成了7条API信息。所以如果没有特殊需求,建议根据实际情况加上requestMethod。

5:Swagger UI面板说明
6:参考
http://blog.didispace.com/springbootswagger2/
http://blog.csdn.net/jia20003/article/details/50700736
Swagger官网 :http://swagger.io/
Spring Boot & Swagger UI : http://fruzenshtein.com/spring-boot-swagger-ui/
Github:https://github.com/swagger-api/swagger-core/wiki/Annotations
---------------------------------------------------------------------------------------
7:接收对象传参的例子
在POJO上增加
package com.zhongying.api.model.base;import io.swagger.annotations.ApiModel;import io.swagger.annotations.ApiModelProperty;/*** 医生对象模型,不要使用该类* @author SUNF**/@ApiModel(value="医生对象模型")public class DemoDoctor{@ApiModelProperty(value="id" ,required=true)private Integer id;@ApiModelProperty(value="医生姓名" ,required=true)private String name;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "DemoDoctor [id=" + id + ", name=" + name + "]";}}
注意: 在后台采用对象接收参数时,Swagger自带的工具采用的是JSON传参, 测试时需要在参数上加入@RequestBody,正常运行采用form或URL提交时候请删除。
- package com.zhongying.api.controller.app;
- import java.util.ArrayList;
- import java.util.List;
- import javax.servlet.http.HttpServletRequest;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.ResponseBody;
- import com.github.pagehelper.PageInfo;
- import com.zhongying.api.exception.HttpStatus401Exception;
- import com.zhongying.api.model.base.DemoDoctor;
- import io.swagger.annotations.Api;
- import io.swagger.annotations.ApiImplicitParam;
- import io.swagger.annotations.ApiImplicitParams;
- import io.swagger.annotations.ApiOperation;
- /**
- * 医生类(模拟)
- * @author SUNF
- */
- @RequestMapping("/api/v1")
- @Controller
- @Api(value = "DoctorTestController-医生信息接口模拟")
- public class DoctorTestController {
- /**
- * 添加医生
- *
- * 在使用对象封装参数进行传参时,需要在该对象添加注解,将其注册到swagger中
- * @link com.zhongying.api.model.base.DemoDoctor
- *
- * 注意: 在后台采用对象接收参数时,Swagger自带的工具采用的是JSON传参,
- * 测试时需要在参数上加入@RequestBody,正常运行采用form或URL提交时候请删除。
- *
- * @param doctor 医生类对象
- * @return
- * @throws Exception
- */
- @ResponseBody
- @RequestMapping(value="/doctor", method= RequestMethod.POST )
- @ApiOperation(value="添加医生信息", notes="")
- public String addDoctor(@RequestBody DemoDoctor doctor) throws Exception{
- if(null == doctor || doctor.getId() == null){
- throw new HttpStatus401Exception("添加医生失败","DT3388","未知原因","请联系管理员");
- }
- try {
- System.out.println("成功----------->"+doctor.getName());
- } catch (Exception e) {
- throw new HttpStatus401Exception("添加医生失败","DT3388","未知原因","请联系管理员");
- }
- return doctor.getId().toString();
- }
- /**
- * 删除医生
- * @param doctorId 医生ID
- * @return
- */
- @ResponseBody
- @RequestMapping(value="/doctor/{doctorId}", method= RequestMethod.DELETE )
- @ApiOperation(value="删除医生信息", notes="")
- @ApiImplicitParam(paramType="query", name = "doctorId", value = "医生ID", required = true, dataType = "Integer")
- public String deleteDoctor(@RequestParam Integer doctorId){
- if(doctorId > 2){
- return "删除失败";
- }
- return "删除成功";
- }
- /**
- * 修改医生信息
- * @param doctorId 医生ID
- * @param doctor 医生信息
- * @return
- * @throws HttpStatus401Exception
- */
- @ResponseBody
- @RequestMapping(value="/doctor/{doctorId}", method= RequestMethod.POST )
- @ApiOperation(value="修改医生信息", notes="")
- @ApiImplicitParam(paramType="query", name = "doctorId", value = "医生ID", required = true, dataType = "Integer")
- public String updateDoctor(@RequestParam Integer doctorId, @RequestBody DemoDoctor doctor) throws HttpStatus401Exception{
- if(null == doctorId || null == doctor){
- throw new HttpStatus401Exception("修改医生信息失败","DT3391","id不能为空","请修改");
- }
- if(doctorId > 5 ){
- throw new HttpStatus401Exception("医生不存在","DT3392","错误的ID","请更换ID");
- }
- System.out.println(doctorId);
- System.out.println(doctor);
- return "修改成功";
- }
- /**
- * 获取医生详细信息
- * @param doctorId 医生ID
- * @return
- * @throws HttpStatus401Exception
- */
- @ResponseBody
- @RequestMapping(value="/doctor/{doctorId}", method= RequestMethod.GET )
- @ApiOperation(value="获取医生详细信息", notes="仅返回姓名..")
- @ApiImplicitParam(paramType="query", name = "doctorId", value = "医生ID", required = true, dataType = "Integer")
- public DemoDoctor getDoctorDetail(@RequestParam Integer doctorId) throws HttpStatus401Exception{
- System.out.println(doctorId);
- if(null == doctorId){
- throw new HttpStatus401Exception("查看医生信息失败","DT3390","未知原因","请联系管理员");
- }
- if(doctorId > 3){
- throw new HttpStatus401Exception("医生不存在","DT3392","错误的ID","请更换ID");
- }
- DemoDoctor doctor = new DemoDoctor();
- doctor.setId(1);
- doctor.setName("测试员");
- return doctor;
- }
- /**
- * 获取医生列表
- * @param pageIndex 当前页数
- * @param pageSize 每页记录数
- * @param request
- * @return
- * @throws HttpStatus401Exception
- */
- @ResponseBody
- @RequestMapping(value="/doctor", method= RequestMethod.GET )
- @ApiOperation(value="获取医生列表", notes="目前一次全部取,不分页")
- @ApiImplicitParams({
- @ApiImplicitParam(paramType="header", name = "token", value = "token", required = true, dataType = "String"),
- @ApiImplicitParam(paramType="query", name = "pageIndex", value = "当前页数", required = false, dataType = "String"),
- @ApiImplicitParam(paramType="query", name = "pageSize", value = "每页记录数", required = true, dataType = "String"),
- })
- public PageInfo<DemoDoctor> getDoctorList(@RequestParam(value = "pageIndex", required = false, defaultValue = "1") Integer pageIndex,
- @RequestParam(value = "pageSize", required = false) Integer pageSize,
- HttpServletRequest request) throws HttpStatus401Exception{
- String token = request.getHeader("token");
- if(null == token){
- throw new HttpStatus401Exception("没有权限","SS8888","没有权限","请查看操作文档");
- }
- if(null == pageSize){
- throw new HttpStatus401Exception("每页记录数不粗安在","DT3399","不存在pageSize","请查看操作文档");
- }
- DemoDoctor doctor1 = new DemoDoctor();
- doctor1.setId(1);
- doctor1.setName("测试员1");
- DemoDoctor doctor2 = new DemoDoctor();
- doctor2.setId(2);
- doctor2.setName("测试员2");
- List<DemoDoctor> doctorList = new ArrayList<DemoDoctor>();
- doctorList.add(doctor1);
- doctorList.add(doctor2);
- return new PageInfo<DemoDoctor>(doctorList);
- }
- }
增加header:
现在很多请求需要在header增加额外参数,可以参考getDoctorList()的做法,使用request接收。
Swagger UI使用指南的更多相关文章
- ABP框架 - Swagger UI 集成
文档目录 本节内容: 简介 Asp.net Core 安装 安装Nuget包 配置 测试 Asp.net 5.x 安装 安装Nuget包 配置 测试 简介 来自它的网页:“...使用一个Swagger ...
- ABP理论学习之Swagger UI集成
返回总目录 本篇目录 介绍 安装 安装Nuget包 配置 测试 介绍 从官方网站上可以看到:"启用了Swagger API,就可以获得交互式文档,生成和发现客户端SDK". 安装 ...
- ASP.NET Core 在 Swagger UI 中显示自定义的 Header Token
Swagger 是个好东西,对于前后端分离的网站来说,不仅是提高前后端开发人员沟通效率的利器,也大大方便了后端人员测试 API.有时候,API 中可能需要在 Header 中设置认证参数,比如 aut ...
- 在Abp中集成Swagger UI功能
在Abp中集成Swagger UI功能 1.安装Swashbuckle.Core包 通过NuGet将Swashbuckle.Core包安装到WebApi项目(或Web项目)中. 2.为WebApi方法 ...
- TP框架整合Swagger UI接口文档
1.下载swagger ui:http://swagger.io/swagger-ui/: 2.在应用目录里新建一个目录xxx:如图 3.解压后把dist目录的所有文件拷贝到新建的目录里面: 4.在新 ...
- 使用 Swagger UI 与 Swashbuckle 创建 RESTful Web API 帮助文件
作者:Sreekanth Mothukuru 2016年2月18日 本文旨在介绍如何使用常用的 Swagger 和 Swashbuckle 框架创建描述 Restful API 的交互界面,并为 AP ...
- Swagger PHP使用指南
先说什么是Swagger, Swagger的使用目的是方便优美的呈现出接口API的各种定义, 生成API文档, 包括参数, 路径之类. 有时后端改了API的参数或者其他设置, 前端直接看这个Swagg ...
- gRPC helloworld service, RESTful JSON API gateway and swagger UI
概述 本篇博文完整讲述了如果通过 protocol buffers 定义并启动一个 gRPC 服务,然后在 gRPC 服务上提供一个 RESTful JSON API 的反向代理 gateway,最后 ...
- ABP 用swagger UI测试API报401无权限访问问题
问题描述: 当我们用swagger UI对Web API 进行测试时报401错误 我们点开GET /api/services/app/Role/GetAll,输入参数 点击Try it out!按钮, ...
随机推荐
- Spring之bean生命始末
可以为Bean定制初始化后的生命行为,也可以为Bean定制销毁前的生命行为.举例:ba06包.首先,这些方法需要在Bean类中事先定义好:是方法名随意的public void方法. 其次,在配置文件的 ...
- git push 时 failed to push some refs 的解决方案
我们在利用 GIt 上传代码的时候,往往会遇到这样一个问题,导致我们的代码没有办法正常上传到仓库中 造成这个问题的原因其实很简单,就是因为远程仓库和本地库不一致. 基于这样的一个问题,解决办法自然也就 ...
- 你的http需要“爱情”
目的是为了更白话的认识http,面对业内人,还有一些吃瓜的... 故事背景描述: 男猪脚在情人节这天给他女票发送了一条信息,"I love U",女主角收到后很开心,也回复了一条信 ...
- jQuery入门——选择器
jQuery选择器可以分为四类:基础选择器,层级选择器,属性选择器,过滤选择器 基础选择器: <!DOCTYPE html> <html> <head> <m ...
- Centos7 系统初试化脚本
系统初始化设置 # 设置主机名,永久修改,再次登陆生效 hostnamectl set-hostname xxxxx # 安装eprl源,常用命令 yum install -y wget && ...
- Linux 中 IDEA 不能调试(Debug)项目
问题描述: can't debug project on idea linux. 在Linux 中, IDEA能运行项目,但是点击调试项目,弹出警告.警告内容如下: Required connecto ...
- C++ 洛谷 P1879 [USACO06NOV]玉米田Corn Fields
没学状压DP的看一下 合法布阵问题 P1879 [USACO06NOV]玉米田Corn Fields 题意:给出一个n行m列的草地(n,m<=12),1表示肥沃,0表示贫瘠,现在要把一些牛放在 ...
- 源码阅读 - java.util.concurrent (一)
java.util.concurrent这个包大致可以分为五个部分: Aomic数据类型 这部分都被放在java.util.concurrent.atomic这个包里面,实现了原子化操作的数据类型,包 ...
- VMware下的Centos7实践Kvm虚拟化(通俗易懂)
虽然网上已经有很多关于kvm安装的教程了,但我还是看得头晕,有的教程里安装的包很多,有的很少,也没说明那些安装包的作用是干嘛的,用的命令也不一样,也没解释命令的意思是什么. 我重新写一个教程,尽量通俗 ...
- 截图编辑器 PicPick Biz v4.1.6/v5.0.3 Lite 绿色便携版
下载地址:点我 PicPick 是由NGWIN 软件科技公司推出的一款实用的.多功能屏幕截图与图像编辑神器.软件具备屏幕截取.取色器.调色板.放大镜.标尺.量角器.坐标轴.白板等功能,支持全屏.活动窗 ...