重生之 SpringBoot3 入门保姆级学习(22、场景整合 Swagger 接口文档)

重生之 SpringBoot3 入门保姆级学习(22、场景整合 Swagger 接口文档)

6.2 Swagger 接口文档


1、将 starter 导入 Maven

  • 官网
https://springdoc.org/

image-20240614165800378

  <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
            <version>2.5.0</version>
  </dependency>

2、浏览器访问地址可以看到我们的接口信息

http://localhost:8080/swagger-ui/index.html

image-20240614165955560

3、可以在网页上对 接口 进行测试

image-20240614170352172

4、修改 RedisTestController 使用 @Tag 注解

  • 常用注解
注解 标注位置 作用
@Tag controller类 标识controller作用
@Parameter 参数 标识参数作用
@Parameters 参数 参数多重说明
@Schema model 层的 JavaBean 描述模型作用及每个属性
@Operation 方法 描述方法作用
@ApiResponse 方法 描述响应状态码等
package com.zhong.redis.controller;

import com.zhong.redis.entity.Person;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @ClassName : RedisTestController
 * @Description :
 * @Author : zhx
 * @Date: 2024-06-14 15:13
 */
@Tag(name = "人员", description = "人员的 CRUD")
@RestController
public class RedisTestController {
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired  // 如果给 redis 中保存数据会使用默认序列化机制,导致 redis 中保存对象不合适
    RedisTemplate redisTemplate;

    @GetMapping("/count")
    public String count() {
        Long count = stringRedisTemplate.opsForValue().increment("count");
        // TODO 常见数据类型 k:v value可以有很多类型
        //
        //  string: 普通字符串:  redisTemplate.opsForValue()
        //  list    列表:       redisTemplate.opsForList()
        //  set:    集合:       redisTemplate.opsForset()
        //  zset:   有序集合:    redisTemplate.opsForzset()
        //  hash:   map结构:    redisTemplate.opsForHash()

        return "访问了[ " + count + " ]次";
    }

    @GetMapping("/person/save")
    public String savePerson() {

        Person person = new Person(1L,"小钟",23,new Date());

        redisTemplate.opsForValue().set("person", person);

        return "ok";
    }

    @GetMapping("/person/get")
    public Person getPerson() {
        return (Person) redisTemplate.opsForValue().get("person");
    }
}

5、显示效果

image-20240614170644679

最近更新

  1. TCP协议是安全的吗?

    2024-06-17 08:44:04       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-17 08:44:04       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-17 08:44:04       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-17 08:44:04       18 阅读

热门阅读

  1. SpringSecurity(JWT、SecurityConfig、Redis)

    2024-06-17 08:44:04       6 阅读
  2. API 类别 - 特效核心

    2024-06-17 08:44:04       5 阅读
  3. Linux 基础IO 三

    2024-06-17 08:44:04       6 阅读
  4. 你应该知道的口语连读技巧

    2024-06-17 08:44:04       6 阅读
  5. Rust创建基准测试bench

    2024-06-17 08:44:04       6 阅读
  6. AWS无服务器 应用程序开发—第十三章 小结2

    2024-06-17 08:44:04       5 阅读
  7. 迁移学习和从头训练(from scratch)的区别

    2024-06-17 08:44:04       6 阅读
  8. Conda编译

    2024-06-17 08:44:04       8 阅读
  9. Linux 常用命令 - userdel 【删除用户】

    2024-06-17 08:44:04       7 阅读
  10. SQL COUNT() 函数深入解析

    2024-06-17 08:44:04       5 阅读
  11. MySQL入门学习-子查询.ALL

    2024-06-17 08:44:04       8 阅读
  12. 快速排序压缩算法2024年最新一种压缩算法

    2024-06-17 08:44:04       7 阅读
  13. 第66集《摄大乘论》

    2024-06-17 08:44:04       7 阅读