当前位置:   article > 正文

SpringBoot整合knife4j_springboot2整合knife4j openapi3

springboot2整合knife4j openapi3

SpringBoot整合knife4j

一、什么是knife4j

在日常开发中,写接口文档是我们必不可少的,而Knife4j就是一个接口文档工具,可以看作是Swagger的升级版,但是界面比Swagger更好看,功能更丰富

早期,swagger-boostrap-ui是1.x版本,如今swagger-bootsrap-ui到2.x,同时也更改名字Knife4j,适用于单体和微服务项目

Knife4j官方网站:https://doc.xiaominfo.com/

二、整合knife4j

2.1 Knife4j配置

  1. 引入Knife4j相关依赖

            <dependency>
                <groupId>com.github.xiaoymin</groupId>
                <artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
                <version>4.4.0</version>
            </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
  2. 配置文件

    spring:
      datasource:
        url: jdbc:mysql://localhost:3306/e-mart?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=utf-8
        driver-class-name: com.mysql.cj.jdbc.Driver
        username: root
        password: root
    # springdoc-openapi项目配置
    springdoc:
      swagger-ui:
        path: /swagger-ui.html
        tags-sorter: alpha
        operations-sorter: alpha
      api-docs:
        path: /v3/api-docs
      group-configs:
        - group: 'default'
          paths-to-match: '/**'
          packages-to-scan: com.bailuo.swagger.controller
    # knife4j的增强配置,不需要增强可以不配
    knife4j:
      enable: true
      setting:
        language: zh_cn
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
  3. 实体类

    package com.bailuo.swagger.entity;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class User {
    
      private long id;
      private String loginName;
      private String userName;
      private String password;
      private long sex;
      private String identityCode;
      private String email;
      private String mobile;
      private long type;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
  4. 创建mapper

    package com.bailuo.swagger.mapper;
    
    import com.bailuo.swagger.entity.User;
    
    import java.util.List;
    
    public interface UserMapper {
        List<User> selectUserList();
        int insertUser(User user);
        int updateUser(User user);
        User selectUser(int userId);
        int deleteUser(int userId);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.bailuo.swagger.mapper.UserMapper">
        <insert id="insertUser">
            insert into easybuy_user(mobile,loginName,userName,sex,identityCode,email,type,password)
            values(#{mobile},#{loginName},#{userName},#{sex},#{identityCode},#{email},#{type},#{password})
        </insert>
        <update id="updateUser">
            update easybuy_user set mobile=#{mobile},loginName=#{loginName},userName=#{userName},sex=#{sex},identityCode=#{identityCode},email=#{email},type=#{type},password=#{password} where id=#{id}
        </update>
        <delete id="deleteUser">
            delete from easybuy_user where id=#{userId}
        </delete>
    
        <select id="selectUserList" resultType="com.bailuo.swagger.entity.User">
            select * from easybuy_user
        </select>
        <select id="selectUser" resultType="com.bailuo.swagger.entity.User">
            select * from easybuy_user where id=#{userId}
        </select>
    </mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
  5. 创建controller

    package com.bailuo.swagger.controller;
    
    
    import com.bailuo.swagger.entity.User;
    import com.bailuo.swagger.service.UserService;
    import com.bailuo.swagger.vo.ResultData;
    import io.swagger.v3.oas.annotations.Operation;
    import io.swagger.v3.oas.annotations.tags.Tag;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    
    import java.util.List;
    
    @RestController
    @RequestMapping("/user")
    @Tag(name = "用户管理接口")
    public class UserController {
        @Autowired
        UserService userService;
        @Operation(summary = "查询所有用户")
        @GetMapping("/")
        public ResultData<List<User>> list(){
            List<User> users = userService.selectUserList();
            return ResultData.success(users);
        }
        @Operation(summary = "修改用户")
        @PutMapping("/")
        public ResultData<Integer> update(@RequestBody User user){
            int resultUpdate = userService.updateUser(user);
            return ResultData.success(resultUpdate);
        }
        @Operation(summary = "查询指定用户")
        @GetMapping("/{id}")
        public ResultData<User> selectById(@PathVariable("id") int id){
            User user = userService.selectUser(id);
            return ResultData.success(user);
        }
        @Operation(summary = "添加用户")
        @PostMapping("/")
        public ResultData<Integer> list(@RequestBody User user){
            int insertResult = userService.insertUser(user);
            return ResultData.success(insertResult);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

2.2 效果

此时运行项目,访问 IP+端口/doc.html
例如:http://127.0.0.1:8080/doc.html
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/891341
推荐阅读
相关标签
  

闽ICP备14008679号