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的介绍和使用的更多相关文章

  1. Spring Boot之Swagger2集成

    一.Swagger2简单介绍 Swagger2,它可以轻松的整合到Spring Boot中,并与Spring MVC程序配合组织出强大RESTful API文档.它既可以减少我们创建文档的工作量,同时 ...

  2. swagger2文档使用

    ①.导入依赖 <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-sw ...

  3. 视频作品《springboot基础篇》上线了

    1.场景描述 第一个视频作品出炉了,<springboot基础篇>上线了,有需要的朋友可以直接点击链接观看.(如需购买,请通过本文链接购买) 2. 课程内容 课程地址:https://ed ...

  4. Logback文件这么配置,TPS提高至少10倍

    来源:https://tinyurl.com/y5zbtgsq 阅读本文,你将了解到 日志输出到文件并根据LEVEL级别将日志分类保存到不同文件 通过异步输出日志减少磁盘IO提高性能 异步输出日志的原 ...

  5. Spring Boot 集成 Swagger 生成 RESTful API 文档

    原文链接: Spring Boot 集成 Swagger 生成 RESTful API 文档 简介 Swagger 官网是这么描述它的:The Best APIs are Built with Swa ...

  6. Spring Boot中使用Swagger2构建RESTful APIs介绍

    1.添加相关依赖 <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> <depen ...

  7. RESTful API的重磅好伙伴Swagger2

    本文将介绍RESTful API的重磅好伙伴Swagger2,它可以轻松的整合到Spring Boot中,并与Spring MVC程序配合组织出强大RESTful API文档. 它既可以减少我们创建文 ...

  8. Spring Boot中使用Swagger2构建强大的RESTful API文档

    由于Spring Boot能够快速开发.便捷部署等特性,相信有很大一部分Spring Boot的用户会用来构建RESTful API.而我们构建RESTful API的目的通常都是由于多终端的原因,这 ...

  9. Spring Boot 2 Swagger2

    本文将介绍RESTful API的重磅好伙伴Swagger2,它可以轻松的整合到Spring Boot中,并与Spring MVC程序配合组织出强大RESTful API文档. 它既可以减少我们创建文 ...

  10. 基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建

    基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建 前言 最近做回后台开发,重新抓起以前学过的SSM(Spring+Sp ...

随机推荐

  1. iview table 左侧固定列 右侧固定列 滚动的时候 表格错位 解决方案

    iview table 左侧固定列 右侧固定列 滚动的时候 表格错位 解决方案 iview table 滚动条位置重置 https://www.jianshu.com/p/32fcd50489ff

  2. vscode 格式化 vue 等文件的 配置 eslint vetur prettier Beautify

    需求 自动格式化需求 多行回车 合并一行,去分号 最后一个逗号,自动删除,符合eslint 结果 虽然能用了,但是 百度好几个方案,也不知道哪个对哪个,太忙没时间弄了. 配置文件记录 eslint 得 ...

  3. iview 日期组件 清空后验证没报红,需要在onChange 进行单独 validateField,因为空字符串校验没有触发

    // 日期组件的 onChange this.yourObj.xxxTime = item this.$refs.yourForm.validateField('xxxTime')

  4. linux脚本免密的方法/不用输入密码

    第一种方法:使用管道(上一个命令的 stdout 接到下一个命令的 stdin):在脚本首行添加 #!/bin/bashecho password | sudo -S apt-get update 第 ...

  5. cpprestsdk有bug.

    好不容易将cpprestsdk移植到MinGW,并编译通过,出于安全还是先将samples还有tests测试一下是否正常. 用samples/blackjack一测试就出现奇葩现象,server一端会 ...

  6. PXE批量安装操作系统自动化

    PXEz自动化 在PXE服务器操作: *yum -y install dhcp xinetd tftp tftp-server* *yum -y install system-config-kicks ...

  7. jQury(事件及其他方法)

    一. jQuery 事件注册 单个事件注册 语法: element.事件(function(){}) $("div").click(function(){ 事件处理程序 }) 其他 ...

  8. 03.Android之View原理问题

    目录介绍 3.0.0.1 View的绘制需要经过哪些过程?有哪些常用回调方法?View的绘制流程的详细流程是怎样的? 3.0.0.2 View绘制流程,当一个TextView的实例调用setText( ...

  9. R语言安装教程

    R 语言官方网站:The Comprehensive R Archive Network 官方镜像站列表:CRAN - Mirrors 一.官网下载R安装包 下载地址为:Index of /bin 进 ...

  10. css实现按钮点击水波纹效果和两边扩散效果

    废话少说,先上代码了,复制到html中即可使用 点击查看代码 <!DOCTYPE html> <html lang="en"> <head> & ...