Swagger2简单使用教程
Swagger2简单使用教程
1、简介
Swagger是为了解决企业中接口(api)中定义统一标准规范的文档生成工具。很多采用前后端分离的模式,前端只负责调用接口,进行渲染,前端和后端的唯一联系,变成了API接口。因此,API文档变得越来越重要。swagger是一个方便我们更好的编写API文档的框架,而且swagger可以模拟http请求调用。
2、常用注解与示例
@Api()用于类:表示标识这个类是swagger的资源
@Api("用于类")
@Controller
public class swaggerTest(){
}
@ApiOperation()用于方法:表示一个http请求的操作
@Api("ApiOperation测试")
@Controller
public class swaggerTest(){
@ApiOperation(value = "apiOperationTest", notes = "apiOperation测试")
public void apiOperationSwaggerTest(){
}
}
@ApiParam():用于方法,参数,字段说明:表示对参数的添加元数据(说明或是否必填等)
@Api("ApiParam测试")
@Controller
public class swaggerTest(){
@ApiOperation(value = "apiOperationTest", notes = "apiOperation测试")
public void apiOperationTest(@ApiParam(name = "id", value = "1", required = true) Integer id){
}
}
@ApiModel()用于类:表示对类进行说明,用于参数用实体类接收
@ApiModel(description = "实体类", value = "实体类")
public class City implements Serializable { }
@ApiModelProperty()用于方法,字段:表示对model属性的说明或者是数据操作更改
@ApiModel(description = "实体类", value = "实体类")
public class City implements Serializable {
@ApiModelProperty(name = "id", value = "编号", required = false, exmaple = "1")
private int id;
}
@ApiIgnore()用于类,方法,方法参数:表示这个方法或者类被忽略
@ApiIgnore
@Api(tags = {"Xxx控制类"})
@RestController
@RequestMapping("/xxx")
public class XxxController { }
@ApiImplicitParam()用于方法:表示单独的请求参数
@ApiImplicitParams()用于方法,包含多个@ApiImplicitParam
@Api("测试1")
@Controller
public class swaggerTest(){
@ApiOperation(value = "apiOperationTest", notes = "apiOperation测试")
@ApiImplicitParams({@ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer", paramType = "query"),
@ApiImplicitParam(name = "name", value = "name", required = true, dataType = "String", paramType = "query")
})
public void apiOperationSwaggerTest(Integer id, String name){
}
}
3、使用步骤
maven导入依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
创建配置类
给出一些基础配置
@Configuration
@EnableSwagger2 //开启Swagger2
public class Swagger2 {
//是否开启swagger,正式环境一般是需要关闭的,可根据springboot的多环境配置进行设置
@Value(value = "${swagger.enabled}")
Boolean swaggerEnabled;
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.xxxxxx.xxxxx")) //你的项目基础包名
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("标题")
.description("api接口文档")
.version("1.0") //版本
.build();
}
}
SpringBoot 配置文件 开启swagger
- application-dev.yml文件
swagger:
enabled: true
注意导包不要导错
import org.springframework.beans.factory.annotation.Value;
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;
实体类demo
@Entity
@Table(name = "city")
public class City implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
@Getter
@Setter
private int ID;
@Column(name = "Name")
@ApiModelProperty(value = "城市名字", dataType = "String", name = "name", example = "Kabul")
@Getter
@Setter
private String Name;
代码中的@Getter@Setter 注解是使用 lombok代替get与set方法,使用方法参考另一篇
service与dao略过 看controller的写法
@ApiOperation(value="按id查询城市信息")
@ResponseBody
@GetMapping("/queryCityList")
public String queryCityList(@RequestParam("id") int id) {
List<City> queryCityList = cityService.queryCityList(id);
String jsonString = JSON.toJSONString(queryCityList);
return jsonString;
}
4、浏览器中使用
http://服务器ip:端口/swagger-ui.html
界面
可以看到刚才我们写的两个方法



Swagger2简单使用教程的更多相关文章
- OpenMP的简单使用教程
转自:http://binglispace.com/2015/01/09/openmp-intro/ OpenMP的简单使用教程 今天有幸参加了一个XSEDE OpenMP的workshop讲座,真是 ...
- 程序员,一起玩转GitHub版本控制,超简单入门教程 干货2
本GitHub教程旨在能够帮助大家快速入门学习使用GitHub,进行版本控制.帮助大家摆脱命令行工具,简单快速的使用GitHub. 做全栈攻城狮-写代码也要读书,爱全栈,更爱生活. 更多原创教程请关注 ...
- knockout简单实用教程3
在之前的文章里面介绍了一些KO的基本用法.包括基本的绑定方式,基本的ko的绑定语法包括text绑定,html绑定等等(如有不明请参照上两篇文章),下面呢介绍一下关于ko的其他方面的知识.包括比较特殊绑 ...
- GitHub这么火,程序员你不学学吗? 超简单入门教程 【转载】
本GitHub教程旨在能够帮助大家快速入门学习使用GitHub. 本文章由做全栈攻城狮-写代码也要读书,爱全栈,更爱生活.原创.如有转载,请注明出处. GitHub是什么? GitHub首先是个分布式 ...
- sea.js简单使用教程
sea.js简单使用教程 下载sea.js, 并引入 官网: http://seajs.org/ github : https://github.com/seajs/seajs 将sea.js导入项目 ...
- vim简单使用教程【转】
vim的学习曲线相当的大(参看各种文本编辑器的学习曲线),所以,如果你一开始看到的是一大堆VIM的命令分类,你一定会对这个编辑器失去兴趣的.下面的文章翻译自<Learn Vim Progress ...
- 简单脱壳教程笔记(2)---手脱UPX壳(1)
本笔记是针对ximo早期发的脱壳基础视频教程,整理的笔记. ximo早期发的脱壳基础视频教程 下载地址如下: http://down.52pojie.cn/%E5%90%BE%E7%88%B1%E7% ...
- 【git】git简单使用教程
git的简单使用教程: 1.安装git bash客户端 2.打开git bash,cd到需要存储代码的路径下, 执行:git clone -b deploy ssh://git@gitlab.xxxx ...
- Flyway 简单入门教程
原文地址:Flyway 简单入门教程 博客地址:http://www.extlight.com 一.前言 Flyway 是一款开源的数据库版本管理工具,它更倾向于规约优于配置的方式.Flyway 可以 ...
随机推荐
- 虚拟机研究系列-「GC本质底层机制」SafePoint的深入分析和底层原理探究指南
SafePoint前提介绍 在高度优化的现代JVM里,Safepoint有几种不同的用法.GC safepoint是最常见.大家听说得最多的,但还有deoptimization safepoint也很 ...
- 微服务网关Ocelot加入IdentityServer4鉴权-.NetCore(.NET5)中使用
Consul+Ocelot+Polly在.NetCore中使用(.NET5)-Consul服务注册,服务发现 Consul+Ocelot+Polly在.NetCore中使用(.NET5)-网关Ocel ...
- css单位px,em,rem区别
在css中单位长度用的最多的是px.em.rem,这三个的区别是: px是固定的像素,一旦设置了就无法因为适应页面大小而改变. em和rem相对于px更具有灵活性,他们是相对长度单位,意思是长度不是定 ...
- 初学python-day3 元组
day2 列表已更新!
- airtext初始化(一)
- 你知道什么是JUC了吗?
多线程一直Java开发中的难点,也是面试中的常客,趁着还有时间,打算巩固一下JUC方面知识,我想机会随处可见,但始终都是留给有准备的人的,希望我们都能加油!!! 沉下去,再浮上来,我想我们会变的不一样 ...
- [技术博客]Unity3d 动画控制
在制作游戏时,导入的箱子模型本身自带动画.然而,它的动画是一个从打开到关闭的完整过程,并且没有给出控制打开关闭的方法. 最直接的想法是对该动画进行拆分,再封装成不同的动画状态,但是不巧的是,这个动画被 ...
- 2021.9.13考试总结[NOIP模拟52]
T1 路径 考虑每一位的贡献,第$i$位每$2^i$个数会变一次,那么答案为$\sum_{i=1}^{log_2n} \frac{n}{2^i}$. $code:$ 1 #include<bit ...
- 2021.8.19考试总结[NOIP模拟44]
T1 emotional flutter 把脚长合到黑条中. 每个黑条可以映射到统一区间,实际操作就是左右端点取模.长度大于$k$时显然不合法. 然后检查一遍区间内有没有不被黑条覆盖的点即可. 区间端 ...
- MVC中单选按钮的实现
-------------控制器-------------- ViewBag.Kinds = SYS_Category.List(xxxxxxxxxxxxxxxxxxxxxxx); --------- ...