1. 什么是swagger2
Swagger2是一个规范和完整的框架,用于生成、描述、调用和可视化RESTful 风格的Web 服务
使用Swagger你只需要按照它的规范去定义接口及接口相关的信息。再通过Swagger衍生出来的一系列项目和工具,就可以做到生成各种格式的接口文档,生成多种语言的客户端和服务端的代码,以及在线接口调试页面等等。这样,如果按照新的开发模式,在开发新版本或者迭代版本的时候,只需要更新Swagger描述文件,就可以自动生成接口文档和客户端服务端代码,做到调用端代码、服务端代码以及接口文档的一致性。
————————————————
版权声明:本文为CSDN博主「猿累人生」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/sinat_23490433/article/details/124186618
2.swagger常用注解
(1)、@Api: 用在请求的类上,例如Controller,表示对类的说明
(2)、@ApiModel: 用在类上,通常是实体类,表示一个返回响应数据的信息
(3)、@ApiModelProperty: 用在属性上,描述响应类的属性
(4)、@ApiOperation: 用在请求的方法上,说明方法的用途、作用
(5)、@ApiImplicitParams: 用在请求的方法上,表示一组参数说明
(6)、@ApiImplicitParam: 用在@ApiImplicitParams注解中,指定一个请求参数的各个方面
3.如何使用接口文档swagger2
3.1引入相关依赖
<!--swagger2依赖-->
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.9.1.RELEASE</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.7.8</version>
</dependency>
3.2创建一个配置类-swagger2
package com.yyj.config;
import org.springframework.context.annotation.Bean;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.VendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import java.util.ArrayList;
/**
* @program: qy164-springboot4
* @description:
* @author: 杨英俊
* @create: 2023-04-12 19:04
**/
public class SwaggerConfig {
@Bean //把方法返回的数据对象 交于spring容器管理
public Docket docket(){
Docket docket = new Docket(DocumentationType.SWAGGER_2).groupName("QY163")
.apiInfo(getInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.yyj.controller")) //只为com.yyj.controller包下的类生成接口文档
.build();
return docket;
}
private ApiInfo getInfo() {
Contact DEFAULT_CONTACT = new Contact("杨英俊", "http://www.baidu.com", "666@qq.com");
ApiInfo apiInfo=new ApiInfo("QY163心里测试系统API", "QY163心里测试系统API", "1.1.0", "http://www.jd.com",
DEFAULT_CONTACT, "志远科技", "http://www.aaa.com", new ArrayList<VendorExtension>());
return apiInfo;
}
}
3.3访问swagger在线文档
4. springboot整合定时器-quartz
4.1什么是quartz
-
一个定时任务调度框架,简单易用,功能强大可以使实现定时任务的。在项目开发过程当中,某些定时任务,可能在运行一段时间之后,就不需要了,或者需要修改下定时任务的执行时间等等。需要在代码当中进行修改然后重新打包发布,很麻烦。使用Quartz来实现的话不需要重新修改代码而达到要求。
-
(1)引入quartz依赖
-
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-quartz</artifactId> </dependency>
-
(2)配置定时器任务
@Component //交于spring容器该类对象
public class QuarzConfig {
@Autowired
private EmpMapper empMapper;
@Scheduled(cron = "0/5 * * * * ? ")
public void show(){
//1.查询过期的订单
List<Emp> emps = empMapper.selectList(null);
System.out.println(emps);
//2.删除过期的订单
}
}
(3)开启定时器注解驱动
springboot整合m
package com.yyj.controller;
import com.yyj.mapper.StudentMapper;
import com.yyj.pojo.entity.Student;
import com.yyj.pojo.vo.StudentVo;
import com.yyj.service.StudentService;
import com.yyj.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* @program: qy164-springboot4
* @description:
* @author: 杨英俊
* @create: 2023-04-12 20:04
**/
@RestController
@RequestMapping("student")
@Api(tags = "学生操作API")
@CrossOrigin
public class StudentController {
@Autowired
private StudentService studentService;
@Autowired
private StudentMapper studentMapper;
@PostMapping("list/{current}/{pageSize}")
@ApiOperation(value = "根据条件分页查询学生信息")
public Result list(
@ApiParam(value = "当前页面",name = "current",required = true,defaultValue = "1")
@PathVariable Integer current,
@ApiParam(value = "每页条数",name = "pageSize",required = true,defaultValue = "5")
@PathVariable Integer pageSize,
@RequestBody StudentVo studentVo
){
return studentService.findByPages(current,pageSize,studentVo);
}
@DeleteMapping("delete/{sid}")
@ApiOperation(value ="根据学生id删除" )
public Result delete(
@ApiParam(name = "sid",value = "学生编号",required = true)
@PathVariable Integer sid
){
int i = studentMapper.deleteById(sid);
return new Result(200,"删除成功",i);
}
@PutMapping("insert")
@ApiOperation(value = "添加数据")
public Result insert(
@ApiParam(name = "student" ,value = "学生信息" ,required = true)
@RequestBody Student student
){
int insert = studentMapper.insert(student);
return new Result(200,"添加成功",insert);
}
@PutMapping("update/{sid}")
@ApiOperation(value = "修改数据")
public Result update(
@ApiParam(name = "student",value = "学生信息",required = true)
@RequestBody Student student
){
int i = studentMapper.updateById(student);
return new Result(200,"修改成功",i);
}
@GetMapping("selectById/{sid}")
@ApiOperation(value = "根据学生编号进行查询")
public Result selectById(
@ApiParam(name = "sid" ,value = "学生编号",required = true)
@PathVariable Integer sid
){
Student student = studentMapper.selectById(sid);
return new Result(200,"查询成功",student);
}
}