首页 > 其他分享 >springboot整合swagger2

springboot整合swagger2

时间:2023-04-17 11:15:38浏览次数:57  
标签:springboot swagger2 API 接口 整合 swagger com public

 

1.正文

1.1 什么是swagger2

Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务的接口文档 . 接口: controller相应的路径方法
Swagger2是一款前后端分离开发中非常实用的API管理工具,它可以帮助开发者根据约定规范自动生成API文档,并支持在线调试API接口,方便开发者快速构建高质量的Restful API。除此之外,Swagger2还支持许多功能,例如对API进行版本管理、验证、授权、模型转换等。它的优点在于简化了API的设计和调试过程,提高了开发效率和代码质量。

1.2 为什么是swagger2

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

1.3 如何使用接口文档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.7.8</version>
        </dependency>

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

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket docket(){
        Docket docket = new Docket(DocumentationType.SWAGGER_2).groupName("QY163")
                .apiInfo(getInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.lht.controller"))
                .build();
        return docket;
    }

    private ApiInfo getInfo(){
        Contact DEFAULT_CONTACT = new Contact("卢海腾", "http://www.baidu.com", "[email protected]");
        ApiInfo apiInfo=new ApiInfo("QY163学生管理系统", "QY163学生管理系统API", "1.1.0", "http://www.jd.com",
                DEFAULT_CONTACT, "志远科技", "http://www.aaa.com", new ArrayList<VendorExtension>());
        return apiInfo;
    }
}

(3)访问swagger在线文档

http://ip:port/swagger-ui.html路径

http://ip:port/doc.html

1.4 swagger中常用的注解

使用swagger注解对接口参数加以说明。

@Api(tags="")====使用在controller类上

@ApiOperation(value="")====接口方法上 接口方法加以说明

@ApiParam(value = "",name = "",required = true)

@ApiModel====实体类

@ApiModelProperty===>实体类的属性说明

2. springboot整合定时器-quartz

https://www.pppet.net/

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

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

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

定时发送短信---->11.11====>

(1)引入quartz依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-quartz</artifactId>
        </dependency>

(2)配置定时器任务

@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.lht.mapper")
@EnableSwagger2
@EnableScheduling //开启定时器注解
public class Lht412SpringbootApplication {

    public static void main(String[] args) {
        SpringApplication.run(Lht412SpringbootApplication.class, args);
    }

}

标签:springboot,swagger2,API,接口,整合,swagger,com,public
From: https://www.cnblogs.com/gl0806/p/17325154.html

相关文章

  • 笔记springboot0410
    1.课程大纲-springboot框架1.什么是Springboot以及Springboot的特点。2.快速搭建springboot项目3.springboot常用的配置文件类型.4.读取springboot配置文件的内容5.多环境配置6.springboot整合数据源。7.springboot整合mybatis.8.springboot整合定时器。2.什么......
  • struts2整合convention插件
    <?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEstrutsPUBLIC"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.1//EN""http://struts.apache.org/dtds/struts-2.1.dtd"><struts><consta......
  • 【Spring Cloud】SpringBoot、Spring Cloud、Spring Cloud Alibaba版本对应
    官方通告SpringBoot1.5.x及以下版本官方不再提供维护了,建议开发者选择使用SpringBoot2.0.x以上的版本,相对应的SpringCloud版本也最好不要使用。简单的查看版本信息:https://start.spring.io/actuator/infoSpringCloud对应的SpringBoot版本访问SpringCloud官网:https://spring......
  • dockerfile的使用,使用dockerfile部署springboot项目
    文章目录一、dockerfile概述1、dockerfile基础2、Docker执行Dockerfile的大致流程3、镜像、容器、dockerfile的关系二、dockerfile常用保留字1、FROM2、MAINTAINER与LABEL3、RUN4、EXPOSE5、WORKDIR6、USER7、ENV8、ADD9、COPY10、VOLUME11、CMD12、ENTRYPOINT三、使用dockerfile构......
  • springcloud或springboot项目服务启动多个实例
    如果没有service,可以快捷键Alt+8,service标签没有信息,则.idea目录下的workspace.xml下替换或添加融化信息内容<componentname="RunDashboard"><optionname="configurationTypes"><set><optionvalue="SpringBootApplicationConfigurationType"/>&......
  • SpringBoot中实现自定义start
    本文主要通过模拟实现redis的功能来自定义start,具体实现口可以往下看1、新建SpringBoot项目,引入依赖<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId>&l......
  • SpringBoot 集成 MybatisPlus 十——数据自动填充
    1自动填充功能介绍自动填充功能可以在插入或修改时为对象属性自动赋值。之前学习了逻辑删除字段,在向数据库插入数据时,都需要设置isDeleted=0,这在进行频繁地数据插入时就显得有些繁琐,于是MybatisPlus就为我们提供了自动填充的功能。修改实体类,为需要自动填充的字段在注解@Table......
  • SpringBoot文件上传大小限制修改
    springboot默认文件上传大小限制为10M如需修改可以参考下面的配置文件spring.servlet.multipart.max-file-size=20MBspring.servlet.multipart.max-request-size=20MB配置说明:Thisconfigurationsetsthemaximumallowedsizeforbothindividualfilesandtheentirere......
  • springboot项目打成jar包后 ,配置文件加载的优先级顺序
    SpringBoot会按照以下顺序来加载配置文件:1、内置默认值:SpringBoot会首先加载内置的默认值,这些默认值定义在SpringBoot的代码中,例如,内置的默认端口号为8080。2、应用级别的配置文件:SpringBoot会从以下位置加载应用级别的配置文件,这些位置按照优先级逐一检查:当前目录下的/c......
  • Java MyBatis-Plus(4)MybatisPlus整合Pagehelper实现分页
    序言 /***pageInfo对象中属性含义*privateintpageNum;//当前页码*privateintpageSize;//设置每页多少条数据*privateintsize;//当前页有多少条数据*privateintstartRow;//当前页码第一条数据的*privateintendRow;//......