swagge2
什么是swagge
swagger2可以在线生成接口文档而且可以对接口进行测试
引入依赖
<!--引入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.9.6</version>
</dependency>
创建配置类
@Configuration
public class SwaggerConfig {
//创建swagger实例
@Bean
public Docket docket() {
Docket docket=
new Docket(DocumentationType.SWAGGER_2)
.apiInfo(getInfo()) //设置接口文档的信息
.select()
.apis(RequestHandlerSelectors.basePackage("com.gym.controller")) //为指定路径下的类生成接口文档
.build();
return docket;
}
private ApiInfo getInfo(){
Contact DEFAULT_CONTACT = new Contact("作者", "网址", "邮箱");
ApiInfo DEFAULT = new ApiInfo("接口名", "该接口作用", "版本", "http://www.baidu.com",
DEFAULT_CONTACT, "单位名", "http://www.jd.com", new ArrayList<VendorExtension>());
return DEFAULT;
}
}
开启swagger注解驱动
@EnableSwagger2 //开启注解驱动
public class 主类Application{
public static void main(String[] args){}
}
swagger2常用注解
1. @Api(tags="") //使用在接口类上,对接口类的说明
2. @ApiOperation(value="") //使用在接口方法上,对接口方法的说明
3. @ApiImplicitParams( //对接口方法所有参数的概述
@ApiImplicitParam(name="参数名",value="参数说明",require="是否必填",dataType="数据类型")
)
4. @ApiModel(value="") //使用在实体类上,对实体类的说明
5. @ApiModelProperty(value="") //使用在实体类属性上,对属性的说明
文档访问方式
定时器
什么是定时器
在指定时间执行的相应业务代码
引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
创建定时业务类
@Configuration
public class QuartzConfig {
@Scheduled(cron = "0/5 * * * * ?") //cron规则
public void show(){
//规则达成后执行的业务代码
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
}
}
开启定时器注解驱动
@EnableScheduling //开启注解驱动
public class 主类Application{
public static void main(String[] args){}
}