• SpringBoot整合knife4j


    1.1 Swagger

    前后端分离开发:前端和后端分开进行开发,2个项目,一个是前端项目,一个是后端项目

    目前基本上很多小项目都是前后端分离,除了后台管理系统

    前后端分离开发,前端(app、小程序、智能硬件等)怎么调用后端接口?怎么知道接口需要什么请求?接口需要什么参数?

    接口文档:描述后端的接口信息,需要明确接口的作用、请求方式、需要的参数、返回的内容、异常说明等,实际中需求会不断发送改变。这样就导致文档随时发送改变,对研发压力就很大。

    实际开发中,我们会采用在线接口文档,以便需求随时改变

    在线接口文档:选择Swagger

    Swagger:免费的一款Java在线接口为文档框架,使用简单,方便

    官网:https://swagger.io/

    1.2 SpringBoot整合Swagger

    提前准备SQL:

    create database db_data2204 char set utf8;
    use db_data2204;
    create table t_project(
      id int primary key auto_increment,
      name varchar(30),
      days int,
      sdate date,
      edate date,
      ctime datetime,
      leader varchar(10)
    );
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    1.依赖

    
    <dependency>
      <groupId>com.github.xiaoymingroupId>
      <artifactId>knife4j-spring-boot-starterartifactId>
      <version>3.0.3version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2.实现配置代码

    @Configuration //标记这是一个配置
    @EnableSwagger2 //启用Swagger
    public class SwaggerConfig {
        //创建接口文档信息
        public ApiInfo createApiInfo(){
            return new ApiInfoBuilder().title("Java2204的接口文档").description("这是我们精英团队历时很长时间写完最有优秀后端接口,好用的一批!").
                contact(new Contact("土狗","http://www.tugou.com","tugou@163.com")).
                version("V1.0.0").build();
        }
        @Bean //IOC容器 存储  
        public Docket createApi(){
            return new Docket(DocumentationType.SWAGGER_2).apiInfo(createApiInfo()).select().
                apis(RequestHandlerSelectors.withClassAnnotation(Api.class)).
                build();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    3.在控制层使用注解标记接口文档信息

    @RestController
    @RequestMapping("/api/project/")
    @Api(tags = "实现项目的相关操作") //swagger 为类添加说明信息
    public class ProjectController {
        @Autowired
        private ProjectService service;
    
        //新增
        @ApiOperation(value = "实现项目的新增",notes = "实现项目的新增")//Swagger 为方法加注释
        @PostMapping("save.do")
        public R save(Project project){
            if(service.save(project)){
                return R.ok();
            }else {
                return R.fail();
            }
        }
        //查询
        @ApiOperation(value = "实现项目的查询",notes = "实现项目的查询")//Swagger 为方法加注释
        @GetMapping("all.do")
        public R all(){
            return R.ok(service.list());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    4.配置文件

    需要配置:

    spring:
        mvc:
            pathmatch:
                  matching-strategy: ANT_PATH_MATCHER
    
    • 1
    • 2
    • 3
    • 4

    5.运行测试

    http://localhost:8082/doc.html

    直接访问doc.html页面

    img

    1.3 Swagger核心

    1.注解

    @Api 修改类,作用:为类添加接口文档的说明

    @ApiOperation 修饰方法,作用:为方法添加说明信息

    @ApiParam 修饰参数,作用:为参数添加说明信息

    @EnableSwagger2 修饰类,作用:启用Swagger,让Swagger生效

    2.配置类

    CV -改 中文说明信息

    @Configuration //标记这是一个配置
    @EnableSwagger2 //启用Swagger
    public class SwaggerConfig {
        //创建接口文档信息
        public ApiInfo createApiInfo(){
            return new ApiInfoBuilder().title("Java2204的接口文档").description("这是我们精英团队历时很长时间写完最有优秀后端接口,好用的一批!").
                    contact(new Contact("土狗","http://www.ccut.com","zhenqk@163.com")).
                    version("V1.0.0").build();
        }
        @Bean //IOC容器 存储  
        public Docket createApi(){
            return new Docket(DocumentationType.SWAGGER_2).apiInfo(createApiInfo()).select().
                    apis(RequestHandlerSelectors.withClassAnnotation(Api.class)).
                    build();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    https://www.apifox.cn/?utm_source=baidu_sem2&bd_vid=12859330887466703508

  • 相关阅读:
    从服务间的一次调用分析整个springcloud的调用过程(二)
    11.动名词
    【Ray】ray.remote和option
    Redis 的线程模型
    Java版分布式微服务云开发架构 Spring Cloud+Spring Boot+Mybatis 电子招标采购系统功能清单
    【MySQL数据库原理】在MySQL Workbench界面运行SQL代码——学生管理系统
    GO语言网络编程(并发编程)Sync
    cadence SPB17.4 - allegro - CAM350_V10.7CN 引入槽孔(.rou)文件报错问题的优雅解决思路
    Android车载蓝牙相关开发4:蓝牙电话操作器BluetoothHeadsetClient
    【一行记录】达梦timestamp转yyyy-mm-dd
  • 原文地址:https://blog.csdn.net/qq_35131055/article/details/128072227