一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Swagger中@ApiIgnore注解的使用詳解

Swagger中@ApiIgnore注解的使用詳解

2022-02-24 00:48周公解碼 Java教程

這篇文章主要介紹了Swagger中@ApiIgnore注解的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

Swagger @ApiIgnore注解的使用

@ApiIgnore 可以用在類、方法上,方法參數中,用來屏蔽某些接口或參數,使其不在頁面上顯示。

1、作用在類上時,整個類都會被忽略

?
1
2
3
4
5
6
7
@ApiIgnore
@Api(tags = {"Xxx控制類"})
@RestController
@RequestMapping("/xxx")
public class XxxController {
  ......
}

隱藏某個類還可以用@Api注解自帶的hidden屬性:

?
1
@Api(value = "xxx", tags = "xxx",hidden = true)

當hidden為true時,該類隱藏。

2、當作用在方法上時,方法將被忽略

?
1
2
3
4
5
6
7
8
9
@ApiIgnore
@ApiOperation(value = "xxx", httpMethod = "POST", notes = "xxx")
@ApiImplicitParams({
  @ApiImplicitParam(name = "xxx", value = "xxx", paramType = "query", dataType = "String", required = true)
})
@PostMapping("/xxx")
public Result importCarryEquExcel(String xxx) {
    ......
}

隱藏某個方法還可以用@APIOperation注解自帶的hidden屬性:

?
1
@ApiOperation(value = "xxx", httpMethod = "GET", notes = "xxx",hidden = true)

當hidden為true時,該方法隱藏。

3、作用在參數上時,單個具體的參數會被忽略

?
1
2
3
public String abc(@ApiIgnore String a, String b, String c){
    return "a" + "b" + "c";
  }

補充:

4、 在實體類中忽略不需要字段的方式

(1)用@ApiModelProperty注解自帶的hidden屬性:

?
1
2
@ApiModelProperty(value = "xxxid", required = true,hidden = true)
private Long id;

(2)使用@JsonIgnore注解:

?
1
2
3
@ApiModelProperty(value = "xxxid", required = true)
@JsonIgnore
private Long id;

包名:

?
1
import  com.fasterxml.jackson.annotation.JsonIgnore;

swagger 注解的使用解析

Swagger簡介

由于架構革新,進入了前后端分離,服務端只需提供RESTful API的時代。

而構建RESTful API會考慮到多終端的問題,這樣就需要面對多個開發人員甚至多個開發團隊。為了減少與其他團隊對接的溝通成本,我們通常會寫好對應的API接口文檔。

從最早開始的word文檔,到后續的showdoc,都能減少很多溝通成本,但隨之帶來的問題也比較麻煩。在開發期間接口會因業務的變更頻繁而變動,如果需要實時更新接口文檔,這是一個費時費力的工作。

為了解決上面的問題,Swagger應運而生。他可以輕松的整合進框架,并通過一系列注解生成強大的API文檔。他既可以減輕編寫文檔的工作量,也可以保證文檔的實時更新,將維護文檔與修改代碼融為一體,是目前較好的解決方案。

常用注解

  • @Api()用于類;
  • 表示標識這個類是swagger的資源
  • @ApiOperation()用于方法;
  • 表示一個http請求的操作
  • @ApiParam()用于方法,參數,字段說明;
  • 表示對參數的添加元數據(說明或是否必填等)
  • @ApiModel()用于類
  • 表示對類進行說明,用于參數用實體類接收
  • @ApiModelProperty()用于方法,字段
  • 表示對model屬性的說明或者數據操作更改
  • @ApiIgnore()用于類,方法,方法參數
  • 表示這個方法或者類被忽略
  • @ApiImplicitParam() 用于方法
  • 表示單獨的請求參數
  • @ApiImplicitParams() 用于方法,包含多個 @ApiImplicitParam

代碼示例

1、@Api

?
1
2
3
@Api(value = "用戶博客", tags = "博客接口")
public class NoticeController {
}

2、@ApiOperation

?
1
2
3
4
5
6
@GetMapping("/detail")
@ApiOperation(value = "獲取用戶詳細信息", notes = "傳入notice" , position = 2)
public R<Notice> detail(Integer id) {
   Notice detail = noticeService.getOne(id);
   return R.data(detail );
}

3、@ApiResponses

?
1
2
3
4
5
6
7
8
@ApiResponses(value = {@ApiResponse(code = 500, msg= "INTERNAL_SERVER_ERROR", response = R.class)})
@GetMapping("/detail")
@ApiOperation(value = "獲取用戶詳細信息", notes = "傳入notice" , position = 2)
@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 );
}

4、@ApiImplicitParams

以分頁代碼進行展示

IPage<Notice> pages = noticeService.page(Condition.getPage(query), Condition.getQueryWrapper(notice, Notice.class));

?
1
2
3
4
5
6
7
8
9
10
@GetMapping("/list")
@ApiImplicitParams({
   @ApiImplicitParam(name = "category", value = "公告類型", paramType = "query", dataType = "integer"),
   @ApiImplicitParam(name = "title", value = "公告標題", paramType = "query", dataType = "string")
})
@ApiOperation(value = "分頁", notes = "傳入notice", position = 3)
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 );
}

5、@ApiParam

?
1
2
3
4
5
6
@PostMapping("/remove")
@ApiOperation(value = "邏輯刪除", notes = "傳入notice", position = 7)
public R remove(@ApiParam(value = "主鍵集合") @RequestParam String ids) {
   boolean temp = noticeService.deleteLogic(Func.toIntList(ids));
   return R.status(temp);
}

6、@ApiModel 與 @ApiModelProperty

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@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;
}

7、@ApiIgnore()

?
1
2
3
4
5
6
@ApiIgnore()
@GetMapping("/detail")
public R<Notice> detail(Integer id) {
   Notice detail = noticeService.getOne(id);
   return R.data(detail );
}

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/u010250240/article/details/103120635

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 网红思瑞一区二区三区 | 美女被狂干| 久久九九亚洲精品 | 小鸟酱喷水 | 新影音先锋男人色资源网 | 亚洲麻豆精品果冻传媒 | 91看片淫黄大片欧美看国产片 | 美女无内裤下部黄 | 国内自拍网红在综合图区 | 免费成年视频 | 青青青在线视频 | 非洲黑人女bbwxxxx | 男人把j放进女人的p里视频 | 国产传媒天美果冻 | 青青青国产视频 | 国产精品一久久香蕉产线看 | 草草视频在线观看 | 韩国美女激情vip | 天天操婷婷 | 亚洲欧美综合区自拍另类 | 欧美人禽杂交狂配无删完整 | 久久性综合亚洲精品电影网 | 性夜影院午夜看片 | 天天做日日爱 | 四虎永久免费地址在线观看 | 秋葵丝瓜茄子草莓榴莲樱桃 | 人成网站在线观看 | 亚洲精品第一国产综合 | 国模李丽莎大尺度啪啪 | 欧美成人免费tv在线播放 | 放荡警察巨r麻麻出轨小说 范冰冰特黄xx大片 饭冈加奈子在线播放观看 法国老妇性xx在线播放 | 日韩免费视频播播 | 色老板最新网站视频地址 | 大乳孕妇一级毛片 | 亚洲瑟瑟网| 成人伊人青草久久综合网破解版 | 色综合色综合 | 日本精品久久久久久久久免费 | 亚洲精品视频观看 | 久久不卡免费视频 | 成人免费福利网站在线看 |