springboot 整合 knife4j-openapi3

适用于:项目已使用shiro安全认证框架,整合knife4j-openapi3

1.引入依赖

<!-- knife4j-openapi3 -->
<dependency>
  <groupId>com.github.xiaoymin</groupId>
  <artifactId>knife4j-openapi3-spring-boot-starter</artifactId>
  <version>4.1.0</version>
</dependency>

2.配置类

package com.xidian.contest.configure;


import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;




@Configuration
public class OpenApiConfig {

    @Bean
    public OpenAPI openAPI() {
        return new OpenAPI()
                .info(new Info().title("竞赛报名管理系统接口文档")
                        .description("竞赛报名管理系统接口文档")
                        .version("v1.0")
                        .contact(new Contact().name("TYJ").url("localhost:8085/login")))
                        .externalDocs(new ExternalDocumentation()
                        .description("Github example code")
                        .url("https://github.com/"));
    }

}

3.yaml配置

# spring-doc 接口文档
springdoc:
  api-docs:
    enabled: true
    #分组配置
  group-configs:
    - group: 'default'
    #匹配的路径
      paths-to-match: '/**'
      #扫描的包
      packages-to-scan: com.xidian.contest.controller
  default-flat-param-object: true
# knife4j的增强配置,不需要增强可以不配
knife4j:
  enable: true
  setting:
    language: zh_cn

4.shiro拦截权限设置

definition.addPathDefinition("/doc.html", "anon");
definition.addPathDefinition("/swagger-resources", "anon");
definition.addPathDefinition("/v3/api-docs/**", "anon");
definition.addPathDefinition("/webjars/**", "anon");
definition.addPathDefinition("/doc.html#/**", "anon");
definition.addPathDefinition("/swagger-ui.html", "anon");
definition.addPathDefinition("/static/**", "anon");

5.启动项目

http://localhost:+端口号+/doc.html



6.常用注解

@Tag

用于说明或定义的标签。
部分参数:
●name:名称
●description:描述


@Schema

用于描述实体类属性的描述、示例、验证规则等,比如 POJO 类及属性。
部分参数:
●name:名称
●title:标题
●description:描述
●example:示例值
●required:是否为必须
●format:属性的格式。如 @Schema(format = "email")
●maxLength 、 minLength:指定字符串属性的最大长度和最小长度
●maximum 、 minimum:指定数值属性的最大值和最小值
●pattern:指定属性的正则表达式模式
●type: 数据类型(integer,long,float,double,string,byte,binary,boolean,date,dateTime,password),必须是字符串。如 @Schema=(type="integer")
●implementation :具体的实现类,可以是类本身,也可以是父类或实现的接口


@Content

内容注解。 部分参数:
●mediaType:内容的类型。比如:application/json
●schema:内容的模型定义,使用 @Schema 注解指定模型的相关信息。

@RequestBody(content = @Content(mediaType = "application/json", schema = @Schema(implementation = User.class))) 
@PostMapping("/users")
public void createUser(User user) {
    // ...
}

@Hidden

某个元素(API 操作、实体类属性等)是否在 API 文档中隐藏。 如,getUserById 方法不会显示在 API 文档中

@Hidden
@GetMapping("/users/{id}")
public User getUserById(@PathVariable("id") Long id) {
    // ...
}

代码参考: 使用在实体类字段中,实现对敏感信息或不需要公开的元素进行隐藏。如:用户密码字段

@Schema(description = "用户")
public class User {
    @Schema(description = "用户id")
    private Long id;

    @Schema(description = "用户名")
    private String name;

    @Hidden
    @Schema(description = "用户密码")
    private String password;

    // ...
}

@Operation

描述 API 操作的元数据信息。常用于 controller 上 部分参数:
●summary:简短描述
●description :更详细的描述
●hidden:是否隐藏
●tags:标签,用于分组API
●operationId:操作的唯一标识符,建议使用唯一且具有描述性的名称
●parameters:指定相关的请求参数,使用 @Parameter 注解来定义参数的详细属性。
●requestBody:指定请求的内容,使用 @RequestBody 注解來指定请求的类型。
●responses:指定操作的返回内容,使用 @ApiResponse 注解定义返回值的详细属性。

@Operation(
  summary = "操作摘要",
  description = "操作的详细描述",
  operationId = "operationId",
  tags = "tag1",
  parameters = {
    @Parameter(name = "param1", description = "参数1", required = true),
    @Parameter(name = "param2", description = "参数2", required = false)
  },
  requestBody = @RequestBody(
    description = "请求描述",
    required = true,
    content = @Content(
      mediaType = "application/json",
      schema = @Schema(implementation = RequestBodyModel.class)
    )
  ),
  responses = {
    @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ResponseModel.class))),
    @ApiResponse(responseCode = "400", description = "错误", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponseModel.class)))
  }
)
@Tag(name = "标签1")
@ApiResponses(value = {
  @ApiResponse(responseCode = "200", description = "成功", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ResponseModel.class))),
  @ApiResponse(responseCode = "400", description = "錯誤", content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponseModel.class)))
})
public void yourOperation() {
  // 逻辑
}

详细参考:

@Operation(summary = "文件分页显示接口")
    @Parameters({
            @Parameter(
                    name = "pageNum",
                    description = "分页数",
                    required = true,
                    in = ParameterIn.QUERY,
                    schema = @Schema(type = "Integer")
            ),
            @Parameter(
                    name = "pageSize",
                    description = "每页大小",
                    required = true,
                    in = ParameterIn.QUERY,
                    schema = @Schema(type = "Integer")
            ),
            @Parameter(
                    name = "name",
                    description = "要查询文件名称",
                    in = ParameterIn.QUERY,
                    schema = @Schema(type = "string")
            )
    })
    @RequiresRoles("admin")
    @GetMapping("/page")
    public IPage<Files> findPage(@RequestParam Integer pageNum,
                                 @RequestParam Integer pageSize,
                                 @RequestParam(defaultValue = "") String name) 

image.png


@Parameter

用于描述 API 操作中的参数 部分参数:
●name : 指定的参数名
●in:参数来源,可选 query、header、path 或 cookie,默认为空,表示忽略
        ○ParameterIn.QUERY 请求参数
        ○ParameterIn.PATH 路径参数
        ○ParameterIn.HEADER header参数
        ○ParameterIn.COOKIE cookie 参数
●description:参数描述
●required:是否必填,默认为 false
●schema :参数的数据类型。如 schema = @Schema(type = "string")
代码参考:

@Parameters({
  @Parameter(
    name = "param1",
    description = "Parameter 1 description",
    required = true,
    in = ParameterIn.PATH,
    schema = @Schema(type = "string")
  ),
  @Parameter(
    name = "param2",
    description = "Parameter 2 description",
    required = true,
    in = ParameterIn.QUERY,
    schema = @Schema(type = "integer")
  )
})


@Parameters

包含多个 @Parameter 注解,指定多个参数。 代码参考: 包含了 param1 和 param2 两个参数
@RequestBody
API 请求的注解
●description:请求信息的描述
●content:请求的内容
●required:是否必须


@ApiResponse

API 的响应信息。 部分参数:
●responseCode:响应的 HTTP 状态码
●description:响应信息的描述
●content:响应的内容
代码参考:

@ApiResponse(responseCode = "200", description = "查询成功", content = @Content(schema = @Schema(implementation = User.class)))
@ApiResponse(responseCode = "404", description = "查询失败")
@GetMapping("/users/{id}")
public ResponseEntity<User> getUserById(@PathVariable("id") Long id) {
    // ...
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/592266.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

SpringBoot+Vue项目在线视频教育平台

一、前言介绍 本系统采用的数据库是Mysql&#xff0c;使用SpringBoot框架开发&#xff0c;运行环境使用Tomcat服务器&#xff0c;idea是本系统的开发平台。在设计过程中&#xff0c;充分保证了系统代码的良好可读性、实用性、易扩展性、通用性、便于后期维护、操作方便以及页面…

ThreeJS:常见几何体与基础材质入门

在前文《ThreeJS:Geometry与顶点|索引|面》中&#xff0c;我们了解了与Geometry几何体相关的基础概念&#xff0c;也尝试了如何通过BufferGeometry自定义几何体。 常见Geometry几何体 ThreeJS内部也提供了诸多封装好的几何体&#xff0c;常见的Geometry几何体如下图所示&#…

Delta lake with Java--利用spark sql操作数据1

今天要解决的问题是如何使用spark sql 建表&#xff0c;插入数据以及查询数据 1、建立一个类叫 DeltaLakeWithSparkSql1&#xff0c;具体代码如下&#xff0c;例子参考Delta Lake Up & Running第3章内容 import org.apache.spark.sql.SaveMode; import org.apache.spark.…

AGI要闻:斯坦福李飞飞首次创业,瞄准“空间智能”;OpenAI下周发布搜索产品挑战谷歌;新的开源 AI 眼镜来了|钛媒体AGI | 最新快讯

多方消息证实&#xff0c;OpenAI将会在北京时间5月10日&#xff08;周五&#xff09;凌晨2点公布搜索引擎新产品消息。 斯坦福大学首位红杉讲席教授 李飞飞 通用人工智能&#xff08;AGI&#xff09;领域又公布了一系列重磅消息。 5月4日凌晨&#xff0c;据路透社&#xff0c…

etcd源码流程---调试环境的搭建

etcd启动命令&#xff1a; name必须设置&#xff0c;否则会用default&#xff0c;集群内不同etcd实例的名字应该是唯一的&#xff0c;因为他会有一个map(name->ip)。如果initial-cluster-state设置为new&#xff0c;那么他会创建一个新的clusterid。需要在initial-cluster中…

算法课程笔记——蓝桥云课第六次直播

&#xff08;只有一个数&#xff0c;或者因子只有一个&#xff09;先自己打表&#xff0c;找找规律函数就是2的n次方 异或前缀和 相等就抵消 先前缀和再二分

离散数学之命题逻辑思维导图+大纲笔记(预习、期末复习,考研,)

大纲笔记&#xff1a; 命题逻辑的基本概念 命题与联结词 命题 命题是推理的基本单位 真命题&#xff0c;假命题 特征 陈述句 唯一的真值 是非真即假的陈述句 非命题 疑问句 祈使句 可真可假 悖论 模糊性 三个基本概念 复合命题 真值取决于原子命题的值和逻辑联结词 原子命题 逻…

树莓派-服务自启配置方式测试

测试脚本&#xff1a; 一、 向rc.local文件添加启动代码(未找到&#xff0c;不测试) 修改/etc/rc.local文件&#xff0c;在文件中exit 0之前添加代码在启动时都会被执行&#xff0c;如&#xff1a;su pi -c “exec /home/pi/testboot.sh” 其中&#xff1a;su pi表示切换至pi…

PR2019新建项目教程

一&#xff0c;新建项目&#xff1a; 设置工程名称&#xff0c;选择工程目录位置&#xff0c;其他默认&#xff1a; 二&#xff0c;新建序列 新建项->序列&#xff1a; 设置序列参数&#xff1a; 三&#xff0c;导出设置 设置导出参数&#xff1a;

C语言 | Leetcode C语言题解之第64题最小路径和

题目&#xff1a; 题解&#xff1a; int minPathSum(int** grid, int gridSize, int* gridColSize) {int rows gridSize, columns gridColSize[0];if (rows 0 || columns 0) {return 0;}int dp[rows][columns];dp[0][0] grid[0][0];for (int i 1; i < rows; i) {dp[i…

【Linux】搭建私有yum仓库(类阿里云)

在搭建本地yum仓库并配置国内镜像阿里云源中了解yum源 yum &#xff1a; Yellow dog Updater&#xff0c;Modified&#xff0c;是一种基于rpm包的自动升级和软件包管理工具。yum能从指定的服务器自动下载rpm包并安装&#xff0c;自动计算出程序之间的依赖关系和软件安装的步骤&…

AST原理(反混淆)

一、AST原理 jscode var a "\u0068\u0065\u006c\u006c\u006f\u002c\u0041\u0053\u0054";在上述代码中&#xff0c;a 是一个变量&#xff0c;它被赋值为一个由 Unicode 转义序列组成的字符串。Unicode 转义序列在 JavaScript 中以 \u 开头&#xff0c;后跟四个十六进…

【Linux入门】基础开发工具

本篇博客整理了Linux&#xff08;centOS版本&#xff09;中基础开发工具的用途和用法&#xff0c;旨在透过开发工具的使用&#xff0c;帮助读者更好地理解可执行程序的编写、编译、运行等。 目录 一、软件包管理器 yum 1.软件的下载与安装 2.Linux应用商店&#xff1a;yum …

【JAVA项目】基于SSM的【寝室管理系统设计】

技术简介&#xff1a;采用B/S架构、ssm 框架和 java 开发的 Web 框架&#xff0c; eclipse开发工具。 系统简介&#xff1a;寝室管理设计的主要使用者分为管理员、宿舍长和学生&#xff0c;实现功能包括管理员权限&#xff1a;首页、个人中心、学生管理、宿舍号管理、宿舍长管理…

使用快捷键的方式把多个关键字文本快速替换(快速替换AE脚本代码)

首先&#xff0c;需要用到的这个工具&#xff1a; 度娘网盘 提取码&#xff1a;qwu2 蓝奏云 提取码&#xff1a;2r1z 这里做AE(Adobe After Effact)里的脚本规则&#xff0c;把英文替换成中文&#xff0c;如下 swap thisComp.layer(“Segment settings”).effect("%&…

谷歌免费的机器学习课程

虽然这样的课程收藏了不少&#xff0c;但是很少有看的下去的&#xff0c;可能我就是这样的收藏党吧。 具体可以跳转链接查看 机器学习工程师学习路径

Springboot(SSM)项目实现数据脱敏

目录 一、引入hutool的依赖 二、sql脚本 三、自定义注解代码 3.1 自定义注解 3.2 自定义一个枚举,用于定义脱敏的类型 3.3 序列化 四、使用脱敏注解 4.1 Person.java 4.2 controller 4.3 dao 五、源代码参考 一、引入hutool的依赖 <dependency><groupId>…

皮内针可以治腱鞘炎吗?如何用皮内针治疗腱鞘炎?

点击文末领取揿针的视频教程跟直播讲解 腕部腱鞘炎是什么&#xff1f; 腱鞘是近关节处的半圆形结构&#xff0c;环形包绕肌腱组织&#xff0c;起到固定肌腱的作用。当关节活动时&#xff0c;肌腱与腱鞘之间会产生相互摩擦&#xff0c;如果两者摩擦过度就会引起炎症&#xff0…

时间复杂度空间复杂度 力扣:转轮数组,消失的数字

1. 算法效率 如何衡量一个算法的好坏&#xff1f;一般是从时间和空间的维度来讨论复杂度&#xff0c;但是现在由于计算机行业发展迅速&#xff0c;所以现在并不怎么在乎空间复杂度了下面例子中&#xff0c;斐波那契看上去很简洁&#xff0c;但是复杂度未必如此 long long Fib…

基于改进暗原色先验和颜色校正的水下图像增强,Matlab实现

博主简介&#xff1a; 专注、专一于Matlab图像处理学习、交流&#xff0c;matlab图像代码代做/项目合作可以联系&#xff08;QQ:3249726188&#xff09; 个人主页&#xff1a;Matlab_ImagePro-CSDN博客 原则&#xff1a;代码均由本人编写完成&#xff0c;非中介&#xff0c;提供…
最新文章