在 Spring Boot 3.x 中使用 SpringDoc 2 / Swagger V3

SpringDoc V1 只支持到 Spring Boot 2.x

springdoc-openapi v1.7.0 is the latest Open Source release supporting Spring Boot 2.x and 1.x.

Spring Boot 3.x 要用 SpringDoc 2 / Swagger V3, 并且包名也改成了 springdoc-openapi-starter-webmvc-ui

SpringDoc V2 https://springdoc.org/v2/

配置

增加 Swagger 只需要在 pom.xml 中添加依賴

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

Spring Boot 啟動時就會自動啟用 Swagger, 從以下地址可以訪問 接口形式(JSON, YAML)和WEB形式的接口文檔

  • http://host:port/context-path/v3/api-docs
    • YAML格式 http://host:port/context-path/v3/api-docs.yaml
  • http://host:port/context-path/swagger-ui/index.html

如果要關閉, 啟用, 自定義接口地址, 在 application.yml 中添加配置

springdoc:api-docs:path: /v3/api-docsenabled: false

對應WEB地址的配置為

springdoc:swagger-ui:path: /swagger-ui.htmlenabled: false

因為WEB界面的顯示基于解析JSON接口返回的結果, 如果api-docs關閉, swagger-ui即使enable也無法使用

在開發和測試環境啟動服務時, 可以用VM參數分別啟用

# in VM arguments
-Dspringdoc.api-docs.enabled=true -Dspringdoc.swagger-ui.enabled=true

使用注解

@Tag

Swagger3 中可以用 @Tag 作為標簽, 將接口方法進行分組. 一般定義在 Controller 上, 如果 Controller 沒用 @Tag 注解, Swagger3 會用Controller的類名作為默認的Tag, 下面例子用 @Tag 定義了一個“Tutorial”標簽, 帶有說明"Tutorial management APIs", 將該標簽應用于TutorialController后, 在 Swagger3 界面上看到的這個 Controller 下面的方法集合就是 Tutorial.

@Tag(name = "Tutorial", description = "Tutorial management APIs")
@RestController
@RequestMapping("/api")
public class TutorialController {//...
}

也可以將 @Tag 添加到單獨的方法上, 這樣在 Swagger3 界面上, 就會將這個方法跟同樣是 Tutorial 標簽的其它方法集合在一起.

public class AnotherController {@Tag(name = "Tutorial", description = "Tutorial APIs")@PostMapping("/tutorials")public ResponseEntity<Tutorial> createTutorial(@RequestBody Tutorial tutorial) {//...}
}

@Operation

Swagger3中 @Operation注解用于單個API方法. 例如

public class MoreController {@Operation(summary = "Retrieve a Tutorial by Id",description = "Some description",tags = { "tutorials", "get" })@GetMapping("/tutorials/{id}")public ResponseEntity<Tutorial> getTutorialById(@PathVariable("id") long id) {//...}
}

tags = { "tutorials", "get" }可以將一個方法放到多個Tag分組中

@ApiResponses 和 @ApiResponse

Swagger3 使用 @ApiResponses 注解標識結果類型列表, 用@ApiResponse注解描述各個類型. 例如

    public class AnotherController {@ApiResponses({@ApiResponse(responseCode = "200",content = { @Content(schema = @Schema(implementation = UserBO.class), mediaType = "application/json") }),@ApiResponse(responseCode = "404",description = "User not found.", content = { @Content(schema = @Schema()) })})@GetMapping("/user/{id}")public ResponseEntity<UserBO> getUserById(@PathVariable("id") long id) {return null;}
}

@Parameter

@Parameter注解用于描述方法參數, 例如:

@GetMapping("/tutorials")
public ResponseEntity<Map<String, Object>> getAllTutorials(@Parameter(description = "Search Tutorials by title") @RequestParam(required = false) String title,@Parameter(description = "Page number, starting from 0", required = true) @RequestParam(defaultValue = "0") int page,@Parameter(description = "Number of items per page", required = true) @RequestParam(defaultValue = "3") int size) {//...
}

@Schema annotation

Swagger3 用 @Schema 注解對象和字段, 以及接口中的參數類型, 例如

@Schema(description = "Tutorial Model Information")
public class Tutorial {@Schema(accessMode = Schema.AccessMode.READ_ONLY, description = "Tutorial Id", example = "123")private long id;@Schema(description = "Tutorial's title", example = "Swagger Tutorial")private String title;// getters and setters
}

accessMode = Schema.AccessMode.READ_ONLY用于在接口定義中標識字段只讀

實例

定義接口

@Tag(name = "CRUD REST APIs for User Resource",description = "CRUD REST APIs - Create User, Update User, Get User, Get All Users, Delete User"
)
@Slf4j
@RestController
public class IndexController {@Operation(summary = "Get a user by its id")@GetMapping(value = "/user_get")public String doGetUser(@Parameter(name = "id", description = "id of user to be searched")@RequestParam(name = "id", required = true)String id) {return "doGetUser: " + id;}@Operation(summary = "Add a user")@PostMapping(value = "/user_add")public String doAddUser(@io.swagger.v3.oas.annotations.parameters.RequestBody(description = "User to add.", required = true)@RequestBody UserBO user) {return "doAddUser: " + user.getName();}@ApiResponses({@ApiResponse(responseCode = "200",content = { @Content(schema = @Schema(implementation = UserBO.class), mediaType = "application/json") }),@ApiResponse(responseCode = "404",description = "User not found.", content = { @Content(schema = @Schema()) })})@GetMapping("/user/{id}")public ResponseEntity<UserBO> getUserById(@PathVariable("id") long id) {return null;}
}

對于這行代碼@io.swagger.v3.oas.annotations.parameters.RequestBody(description = "User to add.", required = true),
因為 Swagger3 的 RequestBody 類和 Spring MVC 的 RequestBody 重名了, 所以在注釋中不得不用完整路徑, 比較影響代碼格式. 在GitHub上有對這個問題的討論(鏈接 https://github.com/swagger-api/swagger-core/issues/3628), 暫時無解.

定義對象

@Schema(description = "UserBO Model Information")
@Data
public class UserBO {@Schema(description = "User ID")private String id;@Schema(description = "User Name")private String name;
}

參考

  • https://www.baeldung.com/spring-rest-openapi-documentation

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/713470.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/713470.shtml
英文地址,請注明出處:http://en.pswp.cn/news/713470.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

select,poll和epoll有什么區別

它們都是NIO中多路復用的三種實現機制&#xff0c;是由linux操作系統提供的。 用戶空間和內核空間&#xff1a;操作系統為了保證系統安全&#xff0c;將內核分為兩個部分&#xff0c;一個是用戶空間&#xff0c;一個是內核空間。用戶空間不能直接訪問底層的硬件設備&#xff0…

IT廉連看——Uniapp——配置文件pages

IT廉連看——Uniapp——配置文件pages [IT廉連看] 本堂課主要為大家介紹pages.json這個配置文件 一、打開官網查看pages.json可以配置哪些屬性。 下面邊寫邊講解 新建一個home頁面理解一下這句話。 以下一些頁面的通用配置 通用設置里我們可以對導航欄和狀態欄進行一些設…

Android修行手冊-集成Python開發環境

Unity3D特效百例案例項目實戰源碼Android-Unity實戰問題匯總游戲腳本-輔助自動化Android控件全解手冊再戰Android系列Scratch編程案例軟考全系列Unity3D學習專欄藍橋系列ChatGPT和AIGC &#x1f449;關于作者 專注于Android/Unity和各種游戲開發技巧&#xff0c;以及各種資源分…

Debezium發布歷史161

原文地址&#xff1a; https://debezium.io/blog/2023/09/13/debezium-2-4-beta2-released/ 歡迎關注留言&#xff0c;我是收集整理小能手&#xff0c;工具翻譯&#xff0c;僅供參考&#xff0c;筆芯筆芯. Debezium 2.4.0.Beta2 Released September 13, 2023 by Chris Cranfo…

Apache Flink連載(三十五):Flink基于Kubernetes部署(5)-Kubernetes 集群搭建-1

?? 個人主頁:IT貧道-CSDN博客 ?? 私聊博主:私聊博主加WX好友,獲取更多資料哦~ ?? 博主個人B棧地址:豹哥教你學編程的個人空間-豹哥教你學編程個人主頁-嗶哩嗶哩視頻 目錄 ?編輯

Python爬蟲——Urllib庫-2

編解碼 問題引入 例如&#xff1a; https://www.baidu.com/s?wd章若楠 https://www.baidu.com/s?wd%E7%AB%A0%E8%8B%A5%E6%A5%A0 第二部分的一串亂碼就是章若楠 如果這里是寫的章若楠就會 產生這樣的錯誤 所以我們就可以使用get請求方式的quote方法了 get請求方式的q…

laravel ApiResponse接口統一響應封裝

一&#xff0c;新增接口返回碼配置文件 在config中新增配置文件apicode.php <?phpreturn [ apicodes>[/*** Message("OK")* 對成功的 GET、PUT、PATCH 或 DELETE 操作進行響應。也可以被用在不創建新資源的 POST 操作上*/HTTP_OK > 200,/*** Message(&qu…

使用el-form之表單校驗自動定位到報錯位置問題,,提升用戶體驗

需求描述 由于需要填寫的表單項太多&#xff0c;提交的時候校驗不通過&#xff0c; 如果沒填寫的表單項在最上面&#xff0c;用戶看不到不知道發生了啥&#xff0c; 所以需要將頁面滾動定位到第一個報錯的表單項位置&#xff0c;提升用戶體驗實現步驟 1. 給form表單添加ref …

數據中心GPU集群高性能組網技術分析

數據中心GPU集群組網技術是指將多個GPU設備連接在一起&#xff0c;形成一個高性能計算的集群系統。通過集群組網技術&#xff0c;可以實現多個GPU設備之間的協同計算&#xff0c;提供更大規模的計算能力&#xff0c;適用于需要大規模并行計算的應用場景。 常用的組網技術&…

1209. 帶分數 刷題筆記

思路 暴力匹配 讀入目標數 n 看n是否與ab/c相等 因為c里面的除法是整除 我們將 nab/c 轉換為 c*na*cb 那么如何獲得a,b&#xff0c;c 依題意 a&#xff0c;b&#xff0c;c三個數由1-9九個數字組成 且每個數字只能出現一次 由此 我們可以搜出123456789的全部排列方式…

我做的app上架應用市場一天,快破400下載量,0差評

上集說到&#xff0c;我做了一個叫QB音樂的安卓app&#xff0c;經過一段時間的自我使用與測試終于算發布了。我昨天順便把它上架了奇妙應用市場&#xff0c;截止目前3月1號過去了一天&#xff0c;下載量快到400&#xff0c;0差評。看來還是能正常使用的。 一、為什么做這個ap…

CleanMyMac X2024免費Mac電腦清理和優化工具

CleanMyMac X是一款專業的 Mac 清理和優化工具&#xff0c;它具備一系列強大的功能&#xff0c;可以幫助用戶輕松管理和維護他們的 Mac 電腦。以下是一些關于 CleanMyMac X 的主要功能和特點&#xff1a; 智能清理&#xff1a;CleanMyMac X 能夠智能識別并清理 Mac 上的無用文件…

深入剖析k8s-Pod篇

為什么需要Pod&#xff1f; 進程是以進程組的方式組織在一起。受限制容器的“單進程模型”&#xff0c; 成組調用沒有被妥善處理&#xff08;資源調用有限&#xff09;&#xff0c;使用資源囤積則導致復雜度上升。 在k8s項目中&#xff0c;Pod的實現需要使用一個中間容器——…

css【詳解】—— 圣杯布局 vs 雙飛翼布局 (含手寫清除浮動 clearfix)

兩者功能效果相同&#xff0c;實現方式不同 效果預覽 兩側寬度固定&#xff0c;中間寬度自適應&#xff08;三欄布局&#xff09;中間部分優先渲染允許三列中的任意一列成為最高列 圣杯布局 通過左右欄填充容器的左右 padding 實現&#xff0c;更多細節詳見注釋。 <!DOCTYP…

《無線網絡技術》考試版筆記

第一章 無線網絡介紹 什么是多徑效應&#xff0c;如何去克服&#xff1a; 在發射機和接收機之間沒有明顯的直線路徑時&#xff0c;就會產生多徑傳播。如果兩個信號彼此疊加&#xff0c;那么接收設備就無法正確解調信號&#xff0c;無法還原為它的原始數據形式。 可以稍微調整接…

【leetcode熱題】求根到葉子節點數字之和

難度&#xff1a; 中等通過率&#xff1a; 40.6%題目鏈接&#xff1a;. - 力扣&#xff08;LeetCode&#xff09; 題目描述 給定一個二叉樹&#xff0c;它的每個結點都存放一個 0-9 的數字&#xff0c;每條從根到葉子節點的路徑都代表一個數字。 例如&#xff0c;從根到葉子…

Linux包管理dpkg、apt和snap

dpkg、apt和snap都是Ubuntu系統中用于軟件管理的工具&#xff0c;但它們在功能和使用上有一些區別。 dpkg: dpkg是Debian包管理系統的底層工具&#xff0c;也是apt和其他高級包管理工具的基礎。主要功能是用于安裝、卸載、配置和構建Debian軟件包&#xff08;.deb文件&#xff…

vue面試題:Computed 和 Watch 的區別?

Computed 和 Watch 的區別? 對于Computed&#xff1a;對于Watch&#xff1a;immediate&#xff1a;組件加載立即觸發回調函數deep&#xff1a;深度監聽&#xff0c;發現數據內部的變化&#xff0c;在復雜數據類型中使用&#xff0c;例如數組中的對象發生變化。需要注意的是&am…

USLE模型-LS因子的計算

目錄 計算坡度計算填洼計算流向計算水流長度計算水平投影![在這里插入圖片描述](https://img-blog.csdnimg.cn/direct/75e015b2d6874ce9b6652f2b8730b90f.png)計算可變的坡度指數m計算坡長因子L計算坡度因子S計算LS因子參考視頻 計算坡度 準備好30米分辨率的dem 計算填洼 計…

速看!深夜悄悄分享一個電力優化代碼集合包!

代碼集合包如下&#xff1a; 主從博弈的智能小區定價策略及電動汽車調度策略 碳交易機制下的綜合能源優化調度 兩階段魯棒優化算法的微網多電源容量配置 冷熱電多能互補綜合能源系統優化調度 考慮預測不確定性的綜合能源調度優化 考慮柔性負荷的綜合能源系統低碳經濟優化調度 考…