Swagger2的介绍和使用
Swagger2介绍
前后端分离开发模式中,api文档是最好的沟通方式。
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。
及时性 (接口变更后,能够及时准确地通知相关前后端开发人员)规范性 (并且保证接口的规范性,如接口的地址,请求方式,参数及响应格式和错误信息)一致性 (接口信息一致,不会出现因开发人员拿到的文档版本不一致,而出现分歧)可测性 (直接在接口文档上进行测试,以方便理解业务)
配置Swagger2
引入相关依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>provided </scope>
</dependency>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<scope>provided </scope>
</dependency>
<!--lombok用来简化实体类:需要安装lombok插件-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided </scope>
</dependency>
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<scope>provided </scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<scope>provided </scope>
</dependency>
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- spring2.X集成redis所需common-pool2
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.0</version>
</dependency>-->
</dependencies>
在模块service-base中,创建swagger的配置类
创建包com.soyoungboy.servicebase.config,创建类SwaggerConfig
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket webApiConfig(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("webApi")
.apiInfo(webApiInfo())
.select()
.paths(Predicates.not(PathSelectors.regex("/admin/.*")))
.paths(Predicates.not(PathSelectors.regex("/error.*")))
.build();
}
private ApiInfo webApiInfo(){
return new ApiInfoBuilder()
.title("网站-课程中心API文档")
.description("本文档描述了课程中心微服务接口定义")
.version("1.0")
.contact(new Contact("Helen", "http://soyoungboy.com", "55317332@qq.com"))
.build();
}
}
在模块service模块中引入service-base
<dependency>
<groupId>com.soyoungboy</groupId>
<artifactId>service-base</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
5、在service-edu启动类上添加注解,进行测试
@SpringBootApplication
@ComponentScan(basePackages = {"com.soyoungboy"})
public class EduApplication {
public static void main(String[] args) {
SpringApplication.run(EduApplication.class, args);
}
}
API模型
可以添加一些自定义设置,例如:
定义样例数据
@ApiModelProperty(value = "创建时间", example = "2019-01-01 8:00:00")
@TableField(fill = FieldFill.INSERT)
private Date gmtCreate;
@ApiModelProperty(value = "更新时间", example = "2019-01-01 8:00:00")
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date gmtModified;

定义接口说明和参数说明
定义在类上:@Api
定义在方法上:@ApiOperation
定义在参数上:@ApiParam
@Api(description="讲师管理")
@RestController
@RequestMapping("/admin/edu/teacher")
public class TeacherAdminController {
@Autowired
private TeacherService teacherService;
@ApiOperation(value = "所有讲师列表")
@GetMapping
public List<Teacher> list(){
return teacherService.list(null);
}
@ApiOperation(value = "根据ID删除讲师")
@DeleteMapping("{id}")
public boolean removeById(
@ApiParam(name = "id", value = "讲师ID", required = true)
@PathVariable String id){
return teacherService.removeById(id);
}
}
Swagger2的介绍和使用的更多相关文章
- Spring Boot之Swagger2集成
一.Swagger2简单介绍 Swagger2,它可以轻松的整合到Spring Boot中,并与Spring MVC程序配合组织出强大RESTful API文档.它既可以减少我们创建文档的工作量,同时 ...
- swagger2文档使用
①.导入依赖 <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-sw ...
- 视频作品《springboot基础篇》上线了
1.场景描述 第一个视频作品出炉了,<springboot基础篇>上线了,有需要的朋友可以直接点击链接观看.(如需购买,请通过本文链接购买) 2. 课程内容 课程地址:https://ed ...
- Logback文件这么配置,TPS提高至少10倍
来源:https://tinyurl.com/y5zbtgsq 阅读本文,你将了解到 日志输出到文件并根据LEVEL级别将日志分类保存到不同文件 通过异步输出日志减少磁盘IO提高性能 异步输出日志的原 ...
- Spring Boot 集成 Swagger 生成 RESTful API 文档
原文链接: Spring Boot 集成 Swagger 生成 RESTful API 文档 简介 Swagger 官网是这么描述它的:The Best APIs are Built with Swa ...
- Spring Boot中使用Swagger2构建RESTful APIs介绍
1.添加相关依赖 <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> <depen ...
- RESTful API的重磅好伙伴Swagger2
本文将介绍RESTful API的重磅好伙伴Swagger2,它可以轻松的整合到Spring Boot中,并与Spring MVC程序配合组织出强大RESTful API文档. 它既可以减少我们创建文 ...
- Spring Boot中使用Swagger2构建强大的RESTful API文档
由于Spring Boot能够快速开发.便捷部署等特性,相信有很大一部分Spring Boot的用户会用来构建RESTful API.而我们构建RESTful API的目的通常都是由于多终端的原因,这 ...
- Spring Boot 2 Swagger2
本文将介绍RESTful API的重磅好伙伴Swagger2,它可以轻松的整合到Spring Boot中,并与Spring MVC程序配合组织出强大RESTful API文档. 它既可以减少我们创建文 ...
- 基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建
基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建 前言 最近做回后台开发,重新抓起以前学过的SSM(Spring+Sp ...
随机推荐
- Zabbix“专家坐诊”第192期问答汇总
问题一 Q:请问下,客户机snmptrap发告警为啥server web收不到,关键是snmptrap日志已经收到,zabbix server配置以及开启snmptrap=1 snmptrap=var ...
- Ubuntu 与Windows 之间搭建共享文件夹
工作中经常需要搭建Linux环境用于测试以及其他开发需求,办公电脑通常是Windows 系统,为便于让文件在两个系统之间传输,可以采取共享文件的方式实现: 1.安装samba 服务: sudo apt ...
- 基于python的wav转txt的源码
最近在做一个算法的时候,用到了这个转换,这里做一个备忘,希望能给你提供价值. import wave import matplotlib.pyplot as plt import numpy as n ...
- 可穿戴心电ECG监测的技术路径及特点
在传统的医疗设备中,监测心跳速率和心脏活动是经由测量电生理讯号与心电图 (ECG) 来完成的,需要将电极连接到身体来量测心脏组织中所引发电气活动的信号.常见的设备用医院的心电图机,长期监护的动态心电仪 ...
- Global AI Bootcamp 成都站 圆满结束!
3月10日星期天下午2点「Global AI Bootcamp 2024 - 成都站」,在成都银泰中心蔚来汽车会议区圆满结束了! 本次活动共计吸引了约50名IT行业从业者线下参与,他们分别来自成都各行 ...
- CSS(三大特性、盒子模型的组成(boder、padding、margin)、ps基本操作)
一.css三大特性 1.层叠性 相同选择器给设置相同的样式,此时一个样式就会覆盖(层叠)另一个冲突的样式.层叠性主要解决样式冲突的问题 层叠性原则: 样式冲突,遵循的原则是就近原则,哪个样式离结构近, ...
- Error: Command failed: C:\windows\system32\cmd.exe /s /c "./configure --disable-shared
错误记录之: Error: Command failed: C:\windows\system32\cmd.exe /s /c "./configure --disable-shared 错 ...
- 用phpStudy配置apache服务器
一:下载phpStudy 1进入官网https://www.xp.cn/download.html 选择稳定的2018版本 2执行下载好的文件,选择下载地址 注意!这里的安装路径不能有中文 3亮两个绿 ...
- 安全测试系列:《web安全深度剖析》读书笔记
一 基础 1.http请求流程: 请求.响应报文格式: 8种请求方式,get(长度有限制).head(用于测试资源是否存在,服务器不返回消息主体).post(传输大量数据).put(给服务器上传资源) ...
- 《.NET内存管理宝典 》(Pro .NET Memory Management) 阅读指南 - 第5章
本章勘误: 暂无,等待细心的你告诉我哦. 本章注解: 出处:207页 原文: 在我们的应用程序中,是不太可能出现LargeHeapHandleTable的问题的.一般来说,可能出现问题会是在需要(动态 ...