Java中Swagger框架的使用詳解
更新時間:2023年08月30日 11:06:02 作者:余生海
這篇文章主要介紹了Java框架Swagger的使用詳解,在開發(fā)期間接口會因業(yè)務的變更頻繁而變動,如果需要實時更新接口文檔,這是一個費時費力的工作,Swagger應運而生,他可以輕松的整合進框架并通過一系列注解生成強大的API文檔,需要的朋友可以參考下
簡介
- 由于架構革新,進入了前后端分離,服務端只需提供RESTful API的時代。
- 而構建RESTful API會考慮到多終端的問題,這樣就需要面對多個開發(fā)人員甚至多個開發(fā)團隊。
- 為了減少與其他團隊對接的溝通成本,我們通常會寫好對應的API接口文檔。
- 從最早開始的word文檔,到后續(xù)的showdoc,都能減少很多溝通成本,但隨之帶來的問題也比較麻煩。在開發(fā)期間接口會因業(yè)務的變更頻繁而變動,如果需要實時更新接口文檔,這是一個費時費力的工作。
- 為了解決上面的問題,Swagger應運而生。他可以輕松的整合進框架,并通過一系列注解生成強大的API文檔。他既可以減輕編寫文檔的工作量,也可以保證文檔的實時更新,將維護文檔與修改代碼融為一體,是目前較好的解決方案。
常用注解
- @Api()用于類;
- 表示標識這個類是swagger的資源
- @ApiOperation()用于方法;
- 表示一個http請求的操作
- @ApiParam()用于方法,參數(shù),字段說明;
- 表示對參數(shù)的添加元數(shù)據(說明或是否必填等)
- @ApiModel()用于類
- 表示對類進行說明,用于參數(shù)用實體類接收
- @ApiModelProperty()用于方法,字段
- 表示對model屬性的說明或者數(shù)據操作更改
- @ApiIgnore()用于類,方法,方法參數(shù)
- 表示這個方法或者類被忽略
- @ApiImplicitParam() 用于方法
- 表示單獨的請求參數(shù)
- @ApiImplicitParams() 用于方法,包含多個 @ApiImplicitParam
代碼示例
- @Api
@Api(value = "用戶博客", tags = "博客接口") public class NoticeController { }
- @ApiOperation
@GetMapping("/detail") @ApiOperation(value = "獲取用戶詳細信息", notes = "傳入notice") public R<Notice> detail(Integer id) { Notice detail = noticeService.getOne(id); return R.data(detail ); }
- @ApiResponses
@GetMapping("/detail") @ApiOperation(value = "獲取用戶詳細信息", notes = "傳入notice") @ApiResponses(value = {@ApiResponse(code = 500, msg= "INTERNAL_SERVER_ERROR", response = R.class)}) public R<Notice> detail(Integer id) { Notice detail = noticeService.getOne(id); return R.data(detail ); }
- @ApiImplicitParams
@GetMapping("/list") @ApiImplicitParams({ @ApiImplicitParam(name = "category", value = "公告類型", paramType = "query", dataType = "integer"), @ApiImplicitParam(name = "title", value = "公告標題", paramType = "query", dataType = "string") }) @ApiOperation(value = "分頁", notes = "傳入notice") public R<IPage<Notice>> list(@ApiIgnore @RequestParam Map<String, Object> notice, Query query) { IPage<Notice> pages = noticeService.page(Condition.getPage(query), Condition.getQueryWrapper(notice, Notice.class)); return R.data(pages ); }
- @ApiParam
@PostMapping("/remove") @ApiOperation(value = "邏輯刪除", notes = "傳入notice") public R remove(@ApiParam(value = "主鍵集合") @RequestParam String ids) { boolean temp = noticeService.deleteLogic(Func.toIntList(ids)); return R.status(temp); }
- @ApiModel 與 @ApiModelProperty
@Data @ApiModel(value = "BladeUser ", description = "用戶對象") public class BladeUser implements Serializable { private static final long serialVersionUID = 1L; @ApiModelProperty(value = "主鍵", hidden = true) private Integer userId; @ApiModelProperty(value = "昵稱") private String userName; @ApiModelProperty(value = "賬號") private String account; @ApiModelProperty(value = "角色id") private String roleId; @ApiModelProperty(value = "角色名") private String roleName; }
- @ApiIgnore()
@ApiIgnore() @GetMapping("/detail") public R<Notice> detail(Integer id) { Notice detail = noticeService.getOne(id); return R.data(detail ); }
到此這篇關于Java中間件Swagger的使用詳解的文章就介紹到這了,更多相關Swagger的使用內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
詳解Java8與Runtime.getRuntime().availableProcessors()
這篇文章主要介紹了詳解Java8與Runtime.getRuntime().availableProcessors(),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06Spring?MVC中JSON數(shù)據處理方式實戰(zhàn)案例
Spring MVC是個靈活的框架,返回JSON數(shù)據的也有很多五花八門的方式,下面這篇文章主要給大家介紹了關于Spring?MVC中JSON數(shù)據處理方式的相關資料,需要的朋友可以參考下2024-01-01windows系統(tǒng)使用mvn命令打包并指定jdk路徑方式
這篇文章主要介紹了windows系統(tǒng)使用mvn命令打包并指定jdk路徑方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04JVM(Java虛擬機)簡介(動力節(jié)點Java學院整理)
Java虛擬機(Jvm)是可運行Java代碼的假想計算機。Java虛擬機包括一套字節(jié)碼指令集、一組寄存器、一個棧、一個垃圾回收堆和一個存儲方法域。對java jvm 虛擬機感興趣的朋友通過本文一起學習吧2017-04-04