• springboot集成swagger3与knife4j


    springboot集成swagger3

    swagger3的springboot启动器jar包

    
    <dependency>
        <groupId>io.springfoxgroupId>
        <artifactId>springfox-boot-starterartifactId>
        <version>3.0.0version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    注意:当前SpringBoot版本为2.5.6,Swagger3.0目前无法完全兼容SpringBoot2.6.x!

    编写TestController代码

    @RestController
    @RequestMapping("test")
    public class TestController {
        @GetMapping
        public Map<String, Object> get(@RequestParam String id) {
            Map<String, Object> r = new HashMap<>(1);
            r.put("id", id);
            return r;
        }
    
        @PostMapping
        public Map<String, Object> post() {
            Map<String, Object> r = new HashMap<>(1);
            r.put("code", 200);
            return r;
        }
        @PutMapping
        public Map<String, Object> put(String id) {
            Map<String, Object> r = new HashMap<>(1);
            r.put("id", id);
            return r;
        }
    
        @DeleteMapping
        public Map<String, Object> delete(String id) {
            Map<String, Object> r = new HashMap<>(1);
            r.put("id", id);
            return r;
        }
    }
    
    • 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

    创建Swagger3Configuration

    @Configuration
    @EnableOpenApi
    public class SwaggerConfig {
        private static final String VERSION = "0.0.1";
        @Bean
        public Docket createRestApi() {
            return new Docket(DocumentationType.OAS_30)
                    .groupName("分组名称")
                    .apiInfo(apiInfo())
                    .select()
                    //要扫描的包
                    .apis(RequestHandlerSelectors.basePackage("com.example.swagger.controller"))
                    .paths(PathSelectors.any())
                    .build();
        }
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    // 设置标题
                    .title("文档标题")
                    //联系人
                    .contact(contact())
                    //描述
                    .description("xxx文档")
                    //服务
                    .termsOfServiceUrl("https://spring.io/")
                    //许可证
                    .license("Apache 2.0")
                    .licenseUrl("https://www.apache.org/licenses/LICENSE-2.0")
                    .version(VERSION)
                    .build();
        }
        private Contact contact (){
            return new Contact("SpringBoot", "https://spring.io/", "email");
        }
    }
    
    • 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

    运行演示

    访问网址:http://127.0.0.1:8081/swagger-ui/index.html

    对接口进行注解

    swagger中常用的注解

    注解作用使用位置
    @Api表示对类的说明常用参数
    @ApiOperation说明方法的用途方法
    @ApiImplicitParams里面可以包含多个@ApiImplicitParam方法
    @ApiImplicitParam描述参数的用途方法
    @ApiModel表示一个数据类的信息
    @ApiModelProperty描述数据类的属性属性
    @ApiIgnore忽略某个字段使之不显示在文档中属性

    接口基本使用

    1.新建一个用户实体类

    @ApiModel("用户")
    @Data
    public class User {
        @ApiModelProperty("用户名")
        private String username;
        @ApiModelProperty("密码")
        private String password;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    2.给TestController添加注解

    @Api(tags = "测试接口")
    @RestController
    @RequestMapping("test")
    public class TestController {
        @ApiOperation("get请求")
        @GetMapping
        @ApiImplicitParam(name = "id", value = "测试用id", dataTypeClass = String.class)
        public Map<String, Object> get(@RequestParam String id) {
            Map<String, Object> r = new HashMap<>(1);
            r.put("id", id);
            return r;
        }
    
        @ApiOperation("post请求")
        @PostMapping
        public Map<String, Object> post(@RequestBody User user) {
            Map<String, Object> r = new HashMap<>(1);
            r.put("code", 200);
            return r;
        }
    
        @ApiOperation("put请求")
        @PutMapping
        @ApiImplicitParam(name = "id", value = "put请求id", dataTypeClass = String.class)
        public Map<String, Object> put(String id) {
            Map<String, Object> r = new HashMap<>(1);
            r.put("id", id);
            return r;
        }
    
        @ApiOperation("delete请求")
        @DeleteMapping
        @ApiImplicitParam(name = "id", value = "delete请求id", dataTypeClass = String.class)
        public Map<String, Object> delete(String id) {
            Map<String, Object> r = new HashMap<>(1);
            r.put("id", id);
            return r;
        }
    }
    
    • 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

    运行结果

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    集成更好的UI-knife4j

    maven

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

    启动器

    在SwaggerConfig类上添加**@EnableKnife4j**

    @Configuration
    @EnableOpenApi
    @EnableKnife4j
    public class SwaggerConfig 
    
    • 1
    • 2
    • 3
    • 4

    访问网站:http://127.0.0.1:8080/doc.html#/home

  • 相关阅读:
    java毕业设计网站SSM幼儿园信息管理系统
    Mysql进阶优化篇06——分组查询优化、分页查询优化、覆盖索引
    终于搞懂什么是动态规划的
    Pointpillar 论文解读
    Go 实现插入排序算法及优化
    ESP8266 WiFi物联网智能插座—项目简介
    【踩坑】PyTorch中指定GPU不生效和GPU编号不一致问题
    【JDK笔记】ParNew回收器:并行回收
    12.OpenWrt-OPKG包管理
    mysql 创建函数
  • 原文地址:https://blog.csdn.net/zhongjianboy/article/details/126366785