Swagger中@ApiIgnore注解的使用詳解
Swagger @ApiIgnore注解的使用
@ApiIgnore 可以用在類、方法上,方法參數(shù)中,用來(lái)屏蔽某些接口或參數(shù),使其不在頁(yè)面上顯示。
1、作用在類上時(shí),整個(gè)類都會(huì)被忽略
@ApiIgnore
@Api(tags = {"Xxx控制類"})
@RestController
@RequestMapping("/xxx")
public class XxxController {
......
}
隱藏某個(gè)類還可以用@Api注解自帶的hidden屬性:
@Api(value = "xxx", tags = "xxx",hidden = true)
當(dāng)hidden為true時(shí),該類隱藏。
2、當(dāng)作用在方法上時(shí),方法將被忽略
@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) {
......
}
隱藏某個(gè)方法還可以用@APIOperation注解自帶的hidden屬性:
@ApiOperation(value = "xxx", httpMethod = "GET", notes = "xxx",hidden = true)
當(dāng)hidden為true時(shí),該方法隱藏。
3、作用在參數(shù)上時(shí),單個(gè)具體的參數(shù)會(huì)被忽略
public String abc(@ApiIgnore String a, String b, String c){
return "a" + "b" + "c";
}
補(bǔ)充:
4、 在實(shí)體類中忽略不需要字段的方式
(1)用@ApiModelProperty注解自帶的hidden屬性:
@ApiModelProperty(value = "xxxid", required = true,hidden = true)
private Long id;
(2)使用@JsonIgnore注解:
@ApiModelProperty(value = "xxxid", required = true)
@JsonIgnore
private Long id;
包名:
import com.fasterxml.jackson.annotation.JsonIgnore;
swagger 注解的使用解析
Swagger簡(jiǎn)介
由于架構(gòu)革新,進(jìn)入了前后端分離,服務(wù)端只需提供RESTful API的時(shí)代。
而構(gòu)建RESTful API會(huì)考慮到多終端的問(wèn)題,這樣就需要面對(duì)多個(gè)開發(fā)人員甚至多個(gè)開發(fā)團(tuán)隊(duì)。為了減少與其他團(tuán)隊(duì)對(duì)接的溝通成本,我們通常會(huì)寫好對(duì)應(yīng)的API接口文檔。
從最早開始的word文檔,到后續(xù)的showdoc,都能減少很多溝通成本,但隨之帶來(lái)的問(wèn)題也比較麻煩。在開發(fā)期間接口會(huì)因業(yè)務(wù)的變更頻繁而變動(dòng),如果需要實(shí)時(shí)更新接口文檔,這是一個(gè)費(fèi)時(shí)費(fèi)力的工作。
為了解決上面的問(wèn)題,Swagger應(yīng)運(yùn)而生。他可以輕松的整合進(jìn)框架,并通過(guò)一系列注解生成強(qiáng)大的API文檔。他既可以減輕編寫文檔的工作量,也可以保證文檔的實(shí)時(shí)更新,將維護(hù)文檔與修改代碼融為一體,是目前較好的解決方案。
常用注解
@Api()用于類;- 表示標(biāo)識(shí)這個(gè)類是swagger的資源
@ApiOperation()用于方法;- 表示一個(gè)http請(qǐng)求的操作
@ApiParam()用于方法,參數(shù),字段說(shuō)明;- 表示對(duì)參數(shù)的添加元數(shù)據(jù)(說(shuō)明或是否必填等)
@ApiModel()用于類- 表示對(duì)類進(jìn)行說(shuō)明,用于參數(shù)用實(shí)體類接收
@ApiModelProperty()用于方法,字段- 表示對(duì)model屬性的說(shuō)明或者數(shù)據(jù)操作更改
@ApiIgnore()用于類,方法,方法參數(shù)- 表示這個(gè)方法或者類被忽略
@ApiImplicitParam()用于方法- 表示單獨(dú)的請(qǐng)求參數(shù)
@ApiImplicitParams()用于方法,包含多個(gè) @ApiImplicitParam
代碼示例
1、@Api
@Api(value = "用戶博客", tags = "博客接口")
public class NoticeController {
}
2、@ApiOperation
@GetMapping("/detail")
@ApiOperation(value = "獲取用戶詳細(xì)信息", notes = "傳入notice" , position = 2)
public R<Notice> detail(Integer id) {
Notice detail = noticeService.getOne(id);
return R.data(detail );
}
3、@ApiResponses
@ApiResponses(value = {@ApiResponse(code = 500, msg= "INTERNAL_SERVER_ERROR", response = R.class)})
@GetMapping("/detail")
@ApiOperation(value = "獲取用戶詳細(xì)信息", 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
以分頁(yè)代碼進(jìn)行展示
IPage<Notice> pages = noticeService.page(Condition.getPage(query), Condition.getQueryWrapper(notice, Notice.class));
@GetMapping("/list")
@ApiImplicitParams({
@ApiImplicitParam(name = "category", value = "公告類型", paramType = "query", dataType = "integer"),
@ApiImplicitParam(name = "title", value = "公告標(biāo)題", paramType = "query", dataType = "string")
})
@ApiOperation(value = "分頁(yè)", 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
@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
@Data
@ApiModel(value = "BladeUser ", description = "用戶對(duì)象")
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 = "賬號(hào)")
private String account;
@ApiModelProperty(value = "角色id")
private String roleId;
@ApiModelProperty(value = "角色名")
private String roleName;
}
7、@ApiIgnore()
@ApiIgnore()
@GetMapping("/detail")
public R<Notice> detail(Integer id) {
Notice detail = noticeService.getOne(id);
return R.data(detail );
}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Maven項(xiàng)目繼承實(shí)現(xiàn)過(guò)程圖解
這篇文章主要介紹了Maven項(xiàng)目繼承實(shí)現(xiàn)過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
MyBatis中使用#{}和${}占位符傳遞參數(shù)的各種報(bào)錯(cuò)信息處理方案
這篇文章主要介紹了MyBatis中使用#{}和${}占位符傳遞參數(shù)的各種報(bào)錯(cuò)信息處理方案,分別介紹了兩種占位符的區(qū)別,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01
Java常用鎖synchronized和ReentrantLock的區(qū)別
這篇文章主要介紹了Java常用鎖synchronized和ReentrantLock的區(qū)別,二者的功效都是相同的,但又有很多不同點(diǎn),下面我們就進(jìn)入文章了解具體的相關(guān)內(nèi)容吧。需要的小伙伴也可以參考一下2022-05-05
java中進(jìn)程與線程_三種實(shí)現(xiàn)方式總結(jié)(必看篇)
下面小編就為大家?guī)?lái)一篇java中進(jìn)程與線程_三種實(shí)現(xiàn)方式總結(jié)(必看篇)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06

