SpringDoc注解解析

一、什么是SpringDoc

SpringDoc注解的使用,它是基于OpenAPI 3和Swagger 3的现代化解决方案,相较于旧版的Swagger2(SpringFox),SpringDoc提供了更简洁、更直观的注解方式。
在这里插入图片描述

二、SpringDoc的注解分类

2.1 作用于类的注解

1. @Tag

用于说明或定义的标签。也可以作用于方法上
部分参数:

name:名称
description:描述

@Tag(name = "用户接口", description = "用户管理相关接口")
@RestController
@RequestMapping("/users")
public class UserController {}

2. @Hidden

某个元素(API操作、实体类属性等)是否在 API 文档中隐藏。当我们想要隐藏某些不必要的信息时,可以使用@Parameter(hidden = true)、@Operation(hidden = true)或者@Hidden注解。

3. @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) {// ...
}

4. @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 :具体的实现类,可以是类本身,也可以是父类或实现的接口。

@Tag(name = "用户", description = "用户实体类")
@Data
public class User {@Schema(name = "用户id", type = "long")private Long id;@Schema(name = "用户名", type = "long")private String name;@Schema(name = "密码", type = "password", minLength = "6", maxLength = "20")private String password;
}

2.2 作用于方法上

1. @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 Operation() {// 逻辑
}

2. @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”)

@Operation(summary = "根据用户名查询用户列表")
@GetMapping("/query/{username}")
public List<User> queryUserByName(@Parameter(name = "username", in = ParameterIn.PATH,description = "用户名",required = true) @PathVariable("username") String userName) {return new ArrayList<>();
}

3. @Parameters

包含多个 @Parameter 注解,指定多个参数。

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

4. @RequestBody @Content

内容注解。

部分参数:

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

@Operation(requestBody = @RequestBody(description = "请求描述",required = true,content = @Content(mediaType = "application/json",schema = @Schema(implementation = RequestBodyModel.class)))
)
public void Operation() {// 逻辑
}

三、场景配置

3.1 类及 pojo 上

@Tag(name = "用户", description = "用户交互载体")
@Data
public class User {@Schema(name = "用户id", type = "string")private String id;@Schema(name = "用户名", type = "string")private String name;@Schema(name = "密码", type = "string")private String password;
}

3.2 Controller 上

@RestController
@RequestMapping("/user")
@Tag(name = "用户管理", description = "用户数据增删改查")
public class UserController {@Autowiredprivate UserService userService;@GetMapping("/{id}")@Operation(summary = "根据ID,查询用户",parameters = {@Parameter(name = "id", required = true, in = ParameterIn.PATH)},responses = {@ApiResponse(responseCode = "200",description = "成功",content = @Content(mediaType = "application/json", schema = @Schema(implementation = User.class))),@ApiResponse(responseCode = "400", description = "错误", content = @Content(mediaType = "application/json"))})public User getUserById(@PathVariable Long id) {return userService.getUserById(id);}
}

3.3 分页场景

	@Operation(description = "searches inventory", operationId = "searchInventory", summary = "By passing in the appropriate options, you can search for available inventory in the system ", tags = {"developers", }, parameters = {@Parameter(description = "pass an optional search string for looking up inventory", name = "searchString") })@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "search results matching criteria"),@ApiResponse(responseCode = "400", description = "bad input parameter") })@GetMapping(value = "/inventory", produces = { "application/json" })ResponseEntity<List<InventoryItem>> searchInventory(@Valid @RequestParam(value = "searchString", required = false) String searchString,// 目标页数必须不是负数@Min(0) @Parameter(description = "number of records to skip for pagination") @Valid @RequestParam(value = "skip", required = true) Integer skip,// 返回的结果数不能大于50条记录@Min(0) @Max(50) @Parameter(description = "maximum number of records to return") @Valid @RequestParam(value = "limit", required = true) Integer limit);

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java:爬虫htmlunit

为什么htmlunit与HttpClient两者都可以爬虫、网页采集、通过网页自动写入数据&#xff0c;我们会推荐使用htmlunit呢? 一、网页的模拟化 首先说说HtmlUnit相对于HttpClient的最明显的一个好处&#xff0c;HtmlUnit更好的将一个网页封装成了一个对象&#xff0c;如果你非要说H…

python-time模块使用

python-time模块使用 1&#xff0c;两个时间概念 struct_time: time模块的时间对象时间戳&#xff1a;从1970年1月1日到现在的秒数&#xff0c;是一个正的浮点数。 2&#xff0c; 日常使用-获取当前时间并将时间按照自己的格式生成字符串 %字符代表变量 %Y 年变量%m 月变量%d …

QT上位机开发(简易图像处理软件)

【 声明&#xff1a;版权所有&#xff0c;欢迎转载&#xff0c;请勿用于商业用途。 联系信箱&#xff1a;feixiaoxing 163.com】 大家都知道图像处理非常地重要&#xff0c;因为它不仅仅是可以用于拍照美颜&#xff0c;而且在工业、医疗和军事等方面也发挥着巨大的作用。另外一…

odoo16 销售模块易错的几个操作

odoo16 销售模块易错的几个操作 据168Report调研团队最新报告“全球定制服装市场报告2023-2029”显示&#xff0c;预计2029年全球定制服装市场规模将达到1082.4亿美元&#xff0c;未来几年年复合增长率CAGR为7.8%。一个普通定制的小皮袄竟月销二十多万件&#xff0c;比我们做定…

SpringBoot-项目引入Redis依赖

在使用Spring Boot开发应用时&#xff0c;可以使用Redis来实现缓存、分布式锁等功能。在编写业务逻辑代码时&#xff0c;可以通过注入RedisTemplate或StringRedisTemplate对象来操作Redis&#xff0c;如存取数据、设置过期时间、删除数据等。同时&#xff0c;还可以使用Redis的…

基于原子搜索算法优化的Elman神经网络数据预测 - 附代码

基于原子搜索算法优化的Elman神经网络数据预测 - 附代码 文章目录 基于原子搜索算法优化的Elman神经网络数据预测 - 附代码1.Elman 神经网络结构2.Elman 神经用络学习过程3.电力负荷预测概述3.1 模型建立 4.基于原子搜索优化的Elman网络5.测试结果6.参考文献7.Matlab代码 摘要&…

力扣-35. 搜索插入位置

文章目录 力扣题目两种解题思路二分查找自己的解题方法--简单易懂 力扣题目 给定一个排序数组和一个目标值&#xff0c;在数组中找到目标值&#xff0c;并返回其索引。如果目标值不存在于数组中&#xff0c;返回它将会被按顺序插入的位置。 请必须使用时间复杂度为 O(log n) …

7+非肿瘤+WGCNA+机器学习+诊断模型,构思巧妙且操作简单

今天给同学们分享一篇生信文章“Platelets-related signature based diagnostic model in rheumatoid arthritis using WGCNA and machine learning”&#xff0c;这篇文章发表在Front Immunol期刊上&#xff0c;影响因子为7.3。 结果解读&#xff1a; DEGs和血小板相关基因的…

VScode 画图插件

开源免费的插件 随着http://draw.io开源vs code插件之后&#xff0c;它一跃成为最强大的流程图工具。 目前http://draw.io支持3种文件后缀&#xff0c;你只需要新建3种后缀之一的文件就可以在vs code中画流程图&#xff0c;它们分别是&#xff1a; *.drawio*.dio*.drawio.sv…

大模型实战营Day1 书生·浦语大模型全链路开源体系

1.大模型为发展通用人工智能的重要途经 专用模型&#xff1a;针对特定任务解决特定问题 通用大模型&#xff1a;一个模型对应多模态多任务 2.InternLM大模型开源历程 3.InternLM-20B大模型性能 4.从模型到应用&#xff1a;智能客服、个人助手、行业应用 5.书生浦语全链条开源…

DynaForm 各版本安装指南

DynaForm下载链接 https://pan.baidu.com/s/1AgsSyjgRi-y0ujRwSwXtHQ?pwd0531 1.鼠标右击【DynaForm5.9.4(64bit)】压缩包&#xff08;win1及以上系统需先点击“显示更多选项”&#xff09;选择【解压到 DynaForm5.9.4(64bit)】。 2.打开解压后的文件夹&#xff0c;鼠标右击…

Vue基础 - v-bind修改属性

<div id"app"> <label for"r1">修改颜色</label><input type"checkbox" v-model"use" id"r1"> <br><br> <div v-bind:class"{class1: use}"> <!--如果use为true…

C++八股学习心得.6

1.C 异常处理 异常是程序在执行期间产生的问题。C 异常是指在程序运行时发生的特殊情况 异常提供了一种转移程序控制权的方式。C 异常处理涉及到三个关键字&#xff1a;try、catch、throw。 throw: 当问题出现时&#xff0c;程序会抛出一个异常。这是通过使用 throw 关键字来…

【数据库】视图索引执行计划多表查询面试题

文章目录 一、视图1.1 概念1.2 视图与数据表的区别1.3 优点1.4 语法1.5 实例 二、索引2.1 什么是索引2.2.为什么要使用索引2.3 优缺点2.4 何时不使用索引2.5 索引何时失效2.6 索引分类2.6.1.普通索引2.6.2.唯一索引2.6.3.主键索引2.6.4.组合索引2.6.5.全文索引 三、执行计划3.1…

@RequestParam,@RequestBody和@PathVariable 区别

RequestParam&#xff0c;RequestBody和PathVariable 这三者是spring常见的接受前端数据的注解&#xff0c;那么他们分别是接受什么的前端数据呢&#xff1f; RequestParam&#xff1a;这个注解主要用于处理请求参数&#xff0c;尤其是GET请求中的查询参数和表单参数。它可以用…

群晖NAS+DMS7.0以上版本+无docker机型安装zerotier

测试机型&#xff1a;群晖synology 218play / DSM版本为7.2.1 因218play无法安装docker&#xff0c;且NAS系统已升级为7.0以上版本&#xff0c;按zerotier官网说法无法安装zerotier, 不过还是可以通过ssh终端和命令方式安装zerotier。 1、在DSM新建文件夹 用于存放zerotier脚…

Java 动态代理是什么? 怎么实现动态代理?

Java 动态代理是什么? 怎么实现动态代理&#xff1f; Java 动态代理是一种在运行时创建代理类和实例的机制&#xff0c;它允许在调用实际方法之前或之后插入自定义的逻辑。动态代理是通过 Java 反射机制实现的&#xff0c;主要利用 java.lang.reflect.Proxy 类和 InvocationH…

HUAWEI WATCH 系列 eSIM 全新开通指南来了

HUAWEI WATCH 系列手表提供了eSIM硬件能力&#xff0c;致力为用户提供更便捷、高效的通信体验。但eSIM 业务是由运营商管理并提供服务的&#xff0c;当前运营商eSIM业务集中全面恢复&#xff0c;电信已经全面恢复&#xff0c;移动大部分省份已经全面放开和多号App开通方式&…

【MATLAB】数豆子

Matlab数豆子 创建一个变量来表示豆子的数量。例如&#xff0c;可以使用豆子数量 100;来表示有100颗豆子。 使用disp函数打印出豆子的数量。例如&#xff0c;可以使用disp([目前有 num2str(豆子数量) 颗豆子])来打印出当前豆子的数量。 进行豆子的计数操作。例如&#xff0c…

RFID标签在汽车监管方面的应用与实施方案

RFID技术在汽车工业领域得到了广泛应用&#xff0c;主要体现在汽车资质证书远程监管系统的普及化&#xff0c;系统包括OBD接口监视器、车证监管箱、超高频读写设备、应用系统软件以及大数据采集与处理等组成部分。 在汽车物流监管方面&#xff0c;系统利用OBD接口监控车辆并实时…