1. SpringBoot集成Swagger

以前,是由后端渲染页面的。现在,前后端的需求越来高,前端需要灵活、炫丽的页面。后端对高并发、高可用、高性能的要求也越来越苛刻。所以前后端自然分道扬镳,各自专注于自己的领域,精益求精!在这样的前后端分离的形态之下,已经变为完全由前端渲染页面了,前端和后端唯一的联系,就是api接口的对接了。api文档变成了前后端开发人员联系的纽带。



1.1 API文档

下面我们以某个项目的“货品模块”为例子,来感受一把前后端分离的api文档的写法。


接口列表



货品列表



货品查询



货品添加



货品修改



货品删除



以上是手动编写API文档的,也就是说,每当后端api发生变化,都需要手动再次修改。相信无论是前端还是后端开发,都或多或少地被接口文档折磨过。前端经常抱怨后端给的接口文档与实际情况不一致。后端又觉得编写及维护接口文档会耗费不少精力,经常来不及更新。其实无论是前端调用后端,还是后端调用后端,都期望有一个好的接口文档。但是这个接口文档对于程序员来说,就跟注释一样,经常会抱怨别人写的代码没有写注释,然而自己写起代码起来,最讨厌的,也是写注释。所以仅仅只通过强制来规范大家是不够的,随着时间推移,版本迭代,接口文档往往很容易就跟不上代码了。像这种机械式的工作,最适合以软件的方式解决了!如此,Swagger应运而生!


1.2 Swagger

Swagger是一个自动生成API文档的工具。能自动生成在线的API文档。也能做功能测试。


1.3 Swagger的组成

Swagger是一组开源项目,其中主要项目如下:

☐ Swagger-tools

☐ Swagger-core

☐ Swagger-js

☐ Swagger-node-express

☐ Swagger-ui

☐ Swagger- codegen


1.4 Swagger环境搭建

导入依赖

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger2</artifactId>

<version>2.9.2</version>

</dependency>

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-swagger-ui</artifactId>

<version>2.9.2</version>

</dependency>

注意,在使用SpringBoot2.7版本的时候,以上的Swagger版本不匹配,启动应用时会报错,所以要在application.yml中添加以下配置:

spring:

mvc:

pathmatch:

matching-strategy:

ant_path_matcher


swagger配置

package com.gao.config;


import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;

import springfox.documentation.service.ApiInfo;

import springfox.documentation.service.Contact;

import springfox.documentation.spi.DocumentationType;

import springfox.documentation.spring.web.plugins.Docket;

import springfox.documentation.swagger2.annotations.EnableSwagger2;


/**

* @author gao

* @time 2022/05/01 16:07:42

*/

@Configuration

@EnableSwagger2

@ComponentScan(basePackages = {"com.gao.web.controller", "com.gao.entity"})

public class SwaggerConfig {

@Bean

public Docket customDocket() {

return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());

}


private ApiInfo apiInfo() {

Contact contact = new Contact("Gao", "http://www.star.com", "64918763@qq.com");

return new ApiInfoBuilder()

小星星API接口")

接口")

.contact(contact)

.version("1.0.0")

.build();

}

}

在启动类上添加:@EnableSwagger2

@SpringBootApplication

@MapperScan("xian.woniuxy.mapper")


@EnableSwagger2

public class App {

public static void main(String[] args) {

SpringApplication.run(App.class, args);

}

}


此时重启应用,然后访问http://localhost:8080/swagger-ui.html,即可看到以下界面

3. SpringBoot工具篇_spring


为什么在创建了com.gao.controller.UserController之后,在Swagger中就能立即显示出它的API呢? 因为在SwaggerConfig类上配置了@ComponentScan(basePackages = {"com.gao.controller"}),告诉Swagger去哪里扫描API信息。


1.5 Swagger注解

☐ @Api 用在类上的注解

☐ @ApiOperation 用在方法上的注解,描述方法

☐ @ApiImplicitParam 用在方法上的注解,描述方法中的参数


在save方法上添加相关的注解

3. SpringBoot工具篇_spring_02


其中:paramType = "body" 用于指定请求参数来自于请求体,dataType = "User" 用于指定save方法参数的类型。注意,在SwaggerConfig配置类上,@ComponentScan一定也要扫描com.gao.entity包,否则会提示找不到dataType中指定的User类的


此时再次查看swagger文档,发现save的api信息如下:

3. SpringBoot工具篇_接口文档_03



此时可以点击“try it out”,来向端点发送请求:

3. SpringBoot工具篇_API_04


点击Execute按钮,即可发送请求,以下是响应信息:

3. SpringBoot工具篇_spring_05



在实体类上也可以添加Swagger注解,如下:

3. SpringBoot工具篇_接口文档_06



此时可以在Swagger文档中看到对实体类属性的解释:

3. SpringBoot工具篇_API_07




2. SpringBoot集成Quartz

在日常项目运行的过程中,我们总会有在某一个时间段周期性执行某个任务的需求。比如每天在某个时间段导出报表,或者每隔多久统计一次在线用户的数量等等。实现“定时执行任务”的技术不止一个,这里主要讲解在SpringBoot中如何使用Quartz这个时钟调度框架。


2.1 使用Quartz

引入依赖

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-quartz</artifactId>

</dependency>


在com.gao.jobs包下创建一个任务类:

3. SpringBoot工具篇_API_08



构建调度配置类(可以推断出,Quartz使用了命令模式)

3. SpringBoot工具篇_spring_09



启动应用,即可看到控制每5秒打印一次系统时间:

3. SpringBoot工具篇_接口文档_10



可以通过以下网站来生成cron表达式:

https://www.matools.com/

3. SpringBoot工具篇_spring_11