百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

SpringDoc OpenAPI 3 常用注解(spring的ioc的常用注解)

nanshan 2024-10-30 02:56 28 浏览 0 评论

SpringBoot 项目整合 SpringDoc OpenAPI 是非常简单的,基本上添加依赖后即可使用,可以参看这篇文章

需要注意的是 springdoc-openapi 支持多个 SpringBoot 版本,不同的 SpringBoot 需要选择对应的 springdoc-openapi 版本。

  • springdoc-openapi v1 支持 SpringBoot v1/v2
  • springdoc-openapi v2 支持 SpringBoot v3

本文介绍 SpringDoc OpenAPI 3,“俗称 Swagger 3” 常见的注解。

SpringDoc 官网地址:https://springdoc.org/


1. OpenApi 3 的主要注解

下表对照 Swagger2 的注解,列出了 OpenAPI 3 的主要注解。

Swagger2

Swagger3 (OpenApi)

@Api

@Tag

@ApiIgnore

@Operation(hidden=true), @Parameter(hidden=true), @Hidden

@ApiModel

@Schema

@ApiModelProperty

@Schema

@ApiOperation(value="简述", notes="详述")

@Operation(summary="简述", description="详述")

@ApiParam

@Parameter

@ApiImplicitParam

@Parameter

@ApiImplicitParams

@Parameters

@ApiResponse(code=404, message="")

@ApiResponse(responseCode="404", description="")


2. @Tag 注解

@Tag 通常用在 Controller 类上,用于说明类作用,也可以作用于方法上。

主要参数:

  • nameTag名称,通常用于 API 分组
  • description:该分组的描述
@Tag(name = "用户管理接口", description = "定义了与用户相关的接口")
@RestController
@RequestMapping("usr")
public class UserController {}


3. @Parameter 和 @Parameters

@Parameter 用于描述 API 的参数,@Parameters 将多个 @Parameter 封装到一起。

主要参数:

  • name:参数名称
  • in:参数来源,默认为空
  • ParameterIn.QUERY 查询参数
  • ParameterIn.PATH 路径参数
  • ParameterIn.HEADER header参数
  • ParameterIn.COOKIE cookie 参数
  • description:参数描述
  • required:是否必填,默认为 false
  • schema :描述参数的数据类型,取值范围等,如 schema = @Schema(type = “string”)
  • hidden:是否隐藏,默认为 false
@GetMapping("findPet")
public String findPet(@Parameter(name = "usrId", 
                          description = "用户ID", 
                          in = ParameterIn.QUERY, 
                          schema = @Schema(type = "integer"), 
                          required=true)
                      @RequestParam Long usrId,                       
                      @Parameter(name = "petId", 
                           description = "宠物ID", 
                           in = ParameterIn.QUERY)
                      @RequestParam(required = false) Long petId) {
    return "elephant";
}

关于“required 是否必填”的几点说明

  • swagger-ui 界面上看到的 “是否为必填”,是由@Parameter(required=true or false) 和 @RequestParam(required=true or false) 共同作用的结果。这也就是有时明明设置了 @Parameter(required=false),但界面上还是显示必填项的原因。
  • ParameterIn.QUERY 类型的参数,如果指定了 defaultValue,也会被标记为 required
  • 路径变量 @PathVariable,是必填的,设置了 @Parameter(required=false) 也不会起作用:


4. 参数的枚举取值范围 allowableValues

可用通过 @Schema(allowableValues = {"cat", "dog"}) 来实现。

@GetMapping("getPetCount")
public Integer getPetCount(@Parameter(name="petType",
                          description="宠物类型",
                          schema=@Schema(type = "string",
                          allowableValues = {"cat", "dog"}))
                          String petType) {
    return 0;
}

5. @Operation 注解

通常用在 Controller 的接口方法上,用于描述接口的名称、请求方法、参数列表,响应代码等。

主要参数:

  • summary:简短描述
  • description :详细描述
  • hidden:是否隐藏
  • tags:用于分组 API,默认与接口所在类上的 @Tag 注解的值相同
  • parameters:指定相关的请求参数,使用 @Parameter 来定义各参数的详细属性。
  • requestBody:指定请求的内容,使用 @RequestBody 來指定请求的类型。
  • responses:指定操作的返回内容,使用 @ApiResponse 注解定义返回值的详细属性。
@Operation(
    summary = "获取指定用户的指定宠物信息",
    description = "根据 usrId 和 petId 获取宠物信息",
    parameters = {
        @Parameter(name = "usrId", description = "用户ID", in = ParameterIn.PATH, schema = @Schema(type = "integer")),
        @Parameter(name = "petId", description = "宠物ID", in = ParameterIn.PATH)},
    responses = {@ApiResponse(responseCode = "200", description = "成功")}
)
@GetMapping("{usrId}/pets/{petId}")
public String getPet(@PathVariable Long usrId, @PathVariable Long petId) {
    log.info("[getPet] {}, {}", usrId, petId);
    return "tiger";
}


6. @ApiResponse 注解

一般用于接口上,说明接口的响应信息,不常使用。

主要参数:

  • responseCode:响应的 HTTP 状态码
  • description:响应信息的描述
  • content:响应的内容
@ApiResponse(responseCode = "200", description = "成功")
public Integer listUsers() {
}

@Operation(summary = "Get thing", responses = {
      @ApiResponse(description = "Successful Operation", responseCode = "200", content = @Content(mediaType = "application/json", schema = @Schema(implementation = String.class))),
      @ApiResponse(responseCode = "404", description = "Not found", content = @Content),
      @ApiResponse(responseCode = "401", description = "Authentication Failure", content = @Content(schema = @Schema(hidden = true))) })
@RequestMapping(path = "/testme", method = RequestMethod.GET)
ResponseEntity<String> testme() {
   return ResponseEntity.ok("Hello");
}


7. @Schema 注解

用于描述实体类及其属性,包括示例、验证规则等。

主要参数:

  • name:名称
  • title:标题
  • description:描述
  • defaultValue:默认值
  • example:示例值
  • type: 数据类型 integer,long,float,double,string,byte,binary,boolean,date、datetime,password
@Schema(title = "User", description = "用户对象")
@Data
public class UserEntity {
    @Schema(description = "序号", example = "1", type = "long")
    private Long seq;
    @Schema(description = "姓名", example = "张三", type = "string")
    private String name;
    @Schema(description = "性别", example = "男", type = "string")
    private String gender;
    @Schema(description = "生日", example = "2000-01-01", type = "string", format = "date")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    private Date birthday;
}

8. @Hidden 注解

@Hidden 用于控制某个元素(API、实体类属性等)在 API 文档中是否要隐藏。与 @Parameter(hidden = false)、@Operation(hidden = false) 作用类似。

相关推荐

Let’s Encrypt免费搭建HTTPS网站

HTTPS(全称:HyperTextTransferProtocoloverSecureSocketLayer),是以安全为目标的HTTP通道,简单讲是HTTP的安全版。即HTTP下加入...

使用Nginx配置TCP负载均衡(nginx tcp负载)

假设Kubernetes集群已经配置好,我们将基于CentOS为Nginx创建一个虚拟机。以下是实验种设置的详细信息:Nginx(CenOS8Minimal)-192.168.1.50Kube...

Nginx负载均衡及支持HTTPS与申请免费SSL证书

背景有两台minio文件服务器已做好集群配置,一台是192.168.56.41:9000;另一台是192.168.56.42:9000。应用程序通过Nginx负载均衡调用这两台minio服务,减轻单点...

HTTPS配置实战(https配置文件)

原因现在网站使用HTTPS是规范操作之一,前些日子买了腾讯云服务,同时申请了域名http://www.asap2me.top/,目前该域名只支持HTTP,想升级为HTTPS。关于HTTPS的链接过程大...

只有IP地址没有域名实现HTTPS访问方法

一般来说,要实现HTTPS,得有个注册好的域名才行。但有时候呢,咱只有服务器的IP地址,没注册域名,这种特殊情况下,也能照样实现HTTPS安全访问,按下面这些步骤来就行:第一步,先确认公网...

超详解:HTTPS及配置Django+HTTPS开发环境

众所周知HTTP协议是以TCP协议为基石诞生的一个用于传输Web内容的一个网络协议,在“网络分层模型”中属于“应用层协议”的一种。在这里我们并不研究该协议标准本身,而是从安全角度去探究使用该协议传输数...

Godaddy购买SSL之后Nginx配置流程以及各种错误的解决

完整流程:参考地址:https://sg.godaddy.com/zh/help/nginx-generate-csrs-certificate-signing-requests-3601生成NGI...

Nginx从安装到高可用,一篇搞定(nginx安装与配置详解)

一、Nginx安装1、去官网http://nginx.org/下载对应的nginx包,推荐使用稳定版本2、上传nginx到linux系统3、安装依赖环境(1)安装gcc环境yuminstallgc...

阿里云免费证书申请,配置安装,使用tomcat,支持http/https访问

参数说明商品类型默认已选择云盾证书服务(无需修改)。云盾证书服务类型SSL证书服务的类型。默认已选择云盾SSL证书(无需修改),表示付费版SSL证书。如果您需要免费领取或付费扩容DV单域名证书【免费试...

你试过两步实现Nginx的规范配置吗?极速生成Nginx配置小工具

NGINX是一款轻量级的Web服务器,最强大的功能之一是能够有效地提供HTML和媒体文件等静态内容。NGINX使用异步事件驱动模型,在负载下提供可预测的性能。是当下最受欢迎的高性能的Web...

从零开始搭建HTTPS服务(搭建https网站)

搭建HTTPS服务的最初目的是为了开发微信小程序,因为wx.request只允许发起HTTPS请求,并且还必须和指定的域名进行网络通信。要从零开始搭建一个HTTPS的服务需要下面4...

群晖NAS使用官网域名和自己的域名配置SSL实现HTTPS访问

安全第一步,群晖NAS使用官网域名和自己的域名配置SSL实现HTTPS访问【新手导向】NAS本质还是一个可以随时随地访问的个人数据存储中心,我们在外网访问的时候,特别是在公网IP下,其实会面临着很多安...

让网站快速升级HTTPS协议提高安全性

为什么用HTTPS网络安全越来越受到重视,很多互联网服务网站,都已经升级改造为https协议。https协议下数据包是ssl/tcl加密的,而http包是明文传输。如果请求一旦被拦截,数据就会泄露产生...

用Https方式访问Harbor-1.9版本(https访问流程)

我上周在头条号写过一篇原创文章《Docker-Harbor&Docker-kitematic史上最详细双系统配置手册》,这篇算是它的姊妹篇吧。这篇文章也将用到我在头条写的另一篇原创文章的...

如何启用 HTTPS 并配置免费的 SSL 证书

在Linux服务器上启用HTTPS并配置免费的SSL证书(以Let'sEncrypt为例)可以通过以下步骤完成:---###**一、准备工作**1.**确保域名已解析**...

取消回复欢迎 发表评论: