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 可以 ...
随机推荐
- 使用 z3 进行逆向 解密字符串
在逆向过程中,我们知道了一个结果值,和一段计算代码.这个时候我们需要知道计算前的值是什么:需要用到 z3 模块来进行解题 z3项目地址 Java代码如下: private String b(Strin ...
- 基于Apache Hudi 的CDC数据入湖
作者:李少锋 文章目录: 一.CDC背景介绍 二.CDC数据入湖 三.Hudi核心设计 四.Hudi未来规划 1. CDC背景介绍 首先我们介绍什么是CDC?CDC的全称是Change data Ca ...
- .NET CLI简单教程和项目结构
WHAT IS .NET CLI ? .NET 命令行接口 (CLI) 工具是用于开发.生成.运行和发布 .NET 应用程序的跨平台工具链. 来源:.NET CLI | Microsoft Docs ...
- [no code][scrum meeting] Alpha 12
项目 内容 会议时间 2020-04-19 会议主题 周总结会议 会议时长 45min 参会人员 全体成员 $( "#cnblogs_post_body" ).catalog() ...
- 微软认真聆听了开源 .NET 开发社区的炮轰: 通过CLI 支持 Hot Reload 功能
微软近日激怒了开源.NET社区,起因是它删除了开源.NET的一项旗舰功能,以提升Visual Studio 的吸引力,尤其是针对与Visual Studio颇有渊源的跨平台源代码编辑器Visual S ...
- 关于把RTL工程代码封装成IP时对define宏定义参数的处理
在把RTL工程封装成IP的时候,如果工程中的代码中含有global include中定义的参数,则vivado不支持该参数文件的封装.出现IP_FLOW 19-4646的错误代码,解决方法: 1.在用 ...
- diff 命令,防止遗忘
常规输出: diff 1.file 2.file 并排格式输出: diff 1.file 2.file -y -W 50 显示说明 "|"表示前后2个文件内容有不同 "& ...
- MongoDB 集群 config server 查询超时导致 mongos 集群写入失败
环境 OS:CentOS 7.x DB:MongoDB 3.6.12 集群模式:mongod-shard1 *3 + mongod-shard2 *3 + mongod-conf-shard *3 + ...
- DDTP 分布式数据传输协议白皮书
声明 本文非本人原创,主要参考文献[1]编写的阅读笔记.本博客仅发表在博客园,作者LightningStar,其他平台均为转载. 摘要 本白皮书对全球现有主要个人信息可携带权的实践模式进行梳理,分析其 ...
- springcloud3(六) 服务降级限流熔断组件Resilience4j
代码地址:https://github.com/showkawa/springBoot_2017/tree/master/spb-demo/spb-gateway/src/test/java/com/ ...