走进springboot (7)

swagger依赖

<!-- swagger依赖--> <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.1</version> </dependency> <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.1</version> </dependency>

SwaggerConfig配置

package com.example.springboot_swagger.config; import io.swagger.annotations.Api; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; 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; import java.util.ArrayList; @Configuration @EnableSwagger2 //开启Swagger public class SwaggerConfig { //配置了Swagger的Bean实例 @Bean public Docket docket(){ return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) // .enable(true) true表示启动swagger false表示不启动swagger .select() // basePackage 表示指定扫描哪个包 .paths(PathSelectors.ant("/example")) // 过滤路径! .build(); // build工厂模式 } // 配置Swagger信息 ApiInfo类 private ApiInfo apiInfo(){ return new ApiInfo("阿辉的Swagger", "Api Documentation", "v1.0", "urn:tos", new Contact("阿辉", "https://www.baidu.com", "1917523457@qq.com"), "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList()); } }

swagger 实现顶部分组功能

不同分组实现不同环境的接口

@Bean public Docket docket(){ // 显示swagger环境 Profiles profiles = Profiles.of("dev","test"); System.out.println(profiles+"dasdas"); return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) // 如何做到多个分组 多个docket实例就可以做到多个分组 及多个docket方法!方法名不同即可 .groupName("阿辉") .select() // basePackage 表示指定扫描哪个包 .paths(PathSelectors.ant("/example")) // 过滤路径! .build(); // build工厂模式 } @Bean public Docket docket2(){ return new Docket(DocumentationType.SWAGGER_2).groupName("Stephen"); } @Bean public Docket docket3(){ return new Docket(DocumentationType.SWAGGER_2).groupName("curry"); }

走进springboot

十三、异步任务实现 service层实现多线程模拟 package com.example.demo.service; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; @Service public class TaskService { @Async // 表示为异步锁! public void ok() throws InterruptedException { Thread.sleep(3000); System.out.println("数据正在处理中……"); } } controller层实现

Controller调用service层

package com.example.demo.controller; import com.example.demo.service.TaskService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class TaskController { @Autowired TaskService taskService; @RequestMapping("/hello") @ResponseBody public String sayHello() throws InterruptedException { taskService.ok(); return "OK"; } }

实现异步任务调度 还需要在主类上开启异步

@EnableAsync

定时任务的执行

corn表达式的执行

需要在主类上加入定时任务执行的注解

@EnableScheduling //开启定时任务

需要在被执行的方法的上面加上

@Scheduled(cron = "0/5 * * * * ?")

package com.example.demo.service; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; @Service public class TaskService { //corn表达式 corn表达式依次顺序 秒 分 时 日 月 星期(可以设置0-7或者? 表示每天) // corn = "30 0/5 10,18 * ?" 表示每天的十点和十八点 每隔五分钟执行一次 //corn = "0 0 12 ? * 1-6" 每个月的周一到周六12点00分执行一次 @Scheduled(cron = "0/5 * * * * ?") //每天每时每刻五秒执行一次 public void hello(){ System.out.println("hello 被执行!"); } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wsxpxx.html