springboot整合swagger2

1.什么是swagger2


Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化RESTful风格的                    Web服务的接口文档。接口:controller相应的路径方法

2.为什么是swagger2

目前的项目基本都是前后端分离,后端为前端提供接口的同时,还需同时提供接口的说明文档。但我们的代码总是会根据实际情况来实时更新,这个时候有可能会忘记更新接口的说明文档,造成一些不必要的问题。

 

 3.如何使用接口文档swagger2

(1)导入swagger依赖

<!--swagger2依赖-->
        <dependency>
            <groupId>com.springboot1</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>

(2)创建一个配置类-swagger2

@Configuration
@EnableSwagger2 //开启swagger注解驱动
public class SwaggerConfig {
 
    @Bean //把方法返回的数据对象 交于spring容器管理
    public Docket docket(){
        Docket docket = new Docket(DocumentationType.SWAGGER_2).groupName("QY163")
                .apiInfo(getInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.tm.controller"))//只为com.tm.controller包下的类生成接口文档
                .build();
 
 
        return docket;
    }
 
 
    public ApiInfo getInfo(){
 
        Contact DEFAULT_CONTACT = new Contact("珊璐", "https://aaa.com", "236@qq.com");
 
        ApiInfo apiInfo = new ApiInfo("QY163心理健康测试", "QY163心理健康测试", "2.3.6", "https://www.jd.com/",
                DEFAULT_CONTACT, "珊璐十八弯", "https://www.aaa.com", new ArrayList());
 
        return apiInfo;
    }
 
 
}

(3)访问swagger在线文档

  4.swagger常用注解


@Api ===== 用在请求的类上,表示类的说明

@ApiOperation =====用在请求的方法上,说明方法的用途,作用

@ApiParam ===== 给接口中的参数定义相关参数说明

@ApiModel ===== 用来对使用该注解的实体类添加额外的描述信息

@ApiModelProperty  ==== 用来对相关实体类中的参数添加额外的描述信息
 

5.spring-boot整合定时器-quartz

定时器:在指定时间执行相应的业务代码

应用场景:比如:定时删除OSS中冗余的文件

三十分钟未支付 ===>取消订单

定时发送短信 比如 双十一11.11

(1)引入quartz依赖

<!--引入quartz依赖 定时器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-quartz</artifactId>
        </dependency>

(2)配置定时器任务

@Component //交于spring容器管理该对象
public class QuartzConfig {
 
 
    @Scheduled(cron = "0/6 * * * * ?")
    public void show(){
        System.out.println("6秒执行一次");
    }
}

(3)开启定时器注解驱动

@SpringBootApplication
//@ComponentScan() //自定义包扫描
//@SpringBootApplication(exclude = "") exclude过滤掉指定的自动装配类
@MapperScan(basePackages = "com.wyl.dao")
@EnableScheduling //开启定时器注解
public class Springboot1Application {
 
    public static void main(String[] args) {
        SpringApplication.run(Springboot1Application.class, args);
    }
 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值