Springboot3
1.srpingboot 整合swagger2
1.1什么是swagger2
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。
1.2为什么要使用swagger2
(1) 及时性 (接口变更后,能够及时准确地通知相关前后端开发人员)
(2)规范性 (并且保证接口的规范性,如接口的地址,请求方式,参数及响应格式和错误信息)
(3)一致性 (接口信息一致,不会出现因开发人员拿到的文档版本不一致,而出现分歧)
(4)可测性 (直接在接口文档上进行测试,以方便理解业务
1.3如何使用接口文档swagger2
(1)添加依赖
(2)创建一个配置类 : SwaggerConfig
@Configuration
@EnableSwagger2//开启swagger注解驱动
public class SwaggerConfig {
@Bean
public Docket docket(){
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.groupName("mqc")
.apiInfo(getInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.mqc.springboot4.controller"))
.build();
return docket;
}
private ApiInfo getInfo(){
Contact DEFAULT_CONTACT = new Contact("马墨林","http://www.baidu.com","123@qq.com");
ApiInfo apiInfo = new ApiInfo("qy163心理测试系统API","qy163心理测试系统API","1.1.0","http://www.baidu.com"
,DEFAULT_CONTACT,"墨木木","http://www.jd.com",new ArrayList<VendorExtension>());
return apiInfo;
}
}
(3)访问swagger在线文档
路径:http://ip:port/swagger-html
http://ip:port/doc.html
1.4 swagger中常用的注解
注解 | 作用 |
---|---|
@Api | 表示对类的说明常用参数 |
@ApiOperation | 说明方法的用途、作用 |
@ApiModel | 表示一个返回响应数据的信息 |
@ApiModelProperty | 描述响应类的属性 |
@ApiIgnore | 忽略某个字段使之不显示在文档中 |
2.springboot整合定时器-quartz
2.1 https://www.pppet.net/
(1)引入quartz依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
(2)配置定时器任务
public class QuarzConfig {
@Component //交于spring容器该类对象
public class QuarzConfig {
@Autowired
private StudentMapper studentMapper;
@Scheduled(cron = "0/5 * * * * ? ")
public void show(){
//1.查询过期的订单
List<Student> students = studentMapper.selectList(null);
System.out.println(students);
//2.删除过期的订单
}
}
}
(3)开启定时器注解驱动
@SpringBootApplication
@MapperScan(basePackages = "com.mqc.springboot4.mapper")
@EnableSwagger2
@EnableScheduling//开启定时器注解
public class SpringBoot4Application {
public static void main(String[] args) {
SpringApplication.run(SpringBoot4Application.class, args);
}
}