SpringDoc注解解析

2024-01-07 08:36
文章标签 解析 注解 springdoc

本文主要是介绍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);}
}

这篇关于SpringDoc注解解析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/579352

相关文章

网页解析 lxml 库--实战

lxml库使用流程 lxml 是 Python 的第三方解析库,完全使用 Python 语言编写,它对 XPath表达式提供了良好的支 持,因此能够了高效地解析 HTML/XML 文档。本节讲解如何通过 lxml 库解析 HTML 文档。 pip install lxml lxm| 库提供了一个 etree 模块,该模块专门用来解析 HTML/XML 文档,下面来介绍一下 lxml 库

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

OWASP十大安全漏洞解析

OWASP(开放式Web应用程序安全项目)发布的“十大安全漏洞”列表是Web应用程序安全领域的权威指南,它总结了Web应用程序中最常见、最危险的安全隐患。以下是对OWASP十大安全漏洞的详细解析: 1. 注入漏洞(Injection) 描述:攻击者通过在应用程序的输入数据中插入恶意代码,从而控制应用程序的行为。常见的注入类型包括SQL注入、OS命令注入、LDAP注入等。 影响:可能导致数据泄

从状态管理到性能优化:全面解析 Android Compose

文章目录 引言一、Android Compose基本概念1.1 什么是Android Compose?1.2 Compose的优势1.3 如何在项目中使用Compose 二、Compose中的状态管理2.1 状态管理的重要性2.2 Compose中的状态和数据流2.3 使用State和MutableState处理状态2.4 通过ViewModel进行状态管理 三、Compose中的列表和滚动

Spring 源码解读:自定义实现Bean定义的注册与解析

引言 在Spring框架中,Bean的注册与解析是整个依赖注入流程的核心步骤。通过Bean定义,Spring容器知道如何创建、配置和管理每个Bean实例。本篇文章将通过实现一个简化版的Bean定义注册与解析机制,帮助你理解Spring框架背后的设计逻辑。我们还将对比Spring中的BeanDefinition和BeanDefinitionRegistry,以全面掌握Bean注册和解析的核心原理。

CSP 2023 提高级第一轮 CSP-S 2023初试题 完善程序第二题解析 未完

一、题目阅读 (最大值之和)给定整数序列 a0,⋯,an−1,求该序列所有非空连续子序列的最大值之和。上述参数满足 1≤n≤105 和 1≤ai≤108。 一个序列的非空连续子序列可以用两个下标 ll 和 rr(其中0≤l≤r<n0≤l≤r<n)表示,对应的序列为 al,al+1,⋯,ar​。两个非空连续子序列不同,当且仅当下标不同。 例如,当原序列为 [1,2,1,2] 时,要计算子序列 [

多线程解析报表

假如有这样一个需求,当我们需要解析一个Excel里多个sheet的数据时,可以考虑使用多线程,每个线程解析一个sheet里的数据,等到所有的sheet都解析完之后,程序需要提示解析完成。 Way1 join import java.time.LocalTime;public class Main {public static void main(String[] args) thro

ZooKeeper 中的 Curator 框架解析

Apache ZooKeeper 是一个为分布式应用提供一致性服务的软件。它提供了诸如配置管理、分布式同步、组服务等功能。在使用 ZooKeeper 时,Curator 是一个非常流行的客户端库,它简化了 ZooKeeper 的使用,提供了高级的抽象和丰富的工具。本文将详细介绍 Curator 框架,包括它的设计哲学、核心组件以及如何使用 Curator 来简化 ZooKeeper 的操作。 1

Unity3D自带Mouse Look鼠标视角代码解析。

Unity3D自带Mouse Look鼠标视角代码解析。 代码块 代码块语法遵循标准markdown代码,例如: using UnityEngine;using System.Collections;/// MouseLook rotates the transform based on the mouse delta./// Minimum and Maximum values can

图解TCP三次握手|深度解析|为什么是三次

写在前面 这篇文章我们来讲解析 TCP三次握手。 TCP 报文段 传输控制块TCB:存储了每一个连接中的一些重要信息。比如TCP连接表,指向发送和接收缓冲的指针,指向重传队列的指针,当前的发送和接收序列等等。 我们再来看一下TCP报文段的组成结构 TCP 三次握手 过程 假设有一台客户端,B有一台服务器。最初两端的TCP进程都是处于CLOSED关闭状态,客户端A打开链接,服务器端