swagger常用注解說明

常用到的注解有:
  • Api
  • ApiModel
  • ApiModelProperty
  • ApiOperation
  • ApiParam
  • ApiResponse
  • ApiResponses
  • ResponseHeader
1. api標(biāo)記

Api 用在類上右核,說明該類的作用。可以標(biāo)記一個Controller類做為swagger 文檔資源携取,使用方式:

@Api(value = "/user", description = "Operations about user")

與Controller注解并列使用卖哎。 屬性配置:

屬性名稱 備注
value url的路徑值
tags 如果設(shè)置這個值、value的值會被覆蓋
description 對api資源的描述
basePath 基本路徑可以不配置
position 如果配置多個Api 想改變顯示的順序位置
produces For example, "application/json, application/xml"
consumes For example, "application/json, application/xml"
protocols Possible values: http, https, ws, wss.
authorizations 高級特性認(rèn)證時配置
hidden 配置為true 將在文檔中隱藏

在SpringMvc中的配置如下:

@Controller
@RequestMapping(value = "/api/pet", produces = {APPLICATION_JSON_VALUE, APPLICATION_XML_VALUE})
@Api(value = "/pet", description = "Operations about pets")
public class PetController {

}
2. ApiOperation標(biāo)記

ApiOperation:用在方法上,說明方法的作用诱鞠,每一個url資源的定義,使用方式:

@ApiOperation(
          value = "Find purchase order by ID",
          notes = "For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions",
          response = Order,
          tags = {"Pet Store"})

與Controller中的方法并列使用互例。
屬性配置:

屬性名稱 備注
value url的路徑值
tags 如果設(shè)置這個值奢入、value的值會被覆蓋
description 對api資源的描述
basePath 基本路徑可以不配置
position 如果配置多個Api 想改變顯示的順序位置
produces For example, "application/json, application/xml"
consumes For example, "application/json, application/xml"
protocols Possible values: http, https, ws, wss.
authorizations 高級特性認(rèn)證時配置
hidden 配置為true 將在文檔中隱藏
response 返回的對象
responseContainer 這些對象是有效的 "List", "Set" or "Map".,其他無效
httpMethod "GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS" and "PATCH"
code http的狀態(tài)碼 默認(rèn) 200
extensions 擴(kuò)展屬性

在SpringMvc中的配置如下:

@RequestMapping(value = "/order/{orderId}", method = GET)
  @ApiOperation(
      value = "Find purchase order by ID",
      notes = "For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions",
      response = Order.class,
      tags = { "Pet Store" })
   public ResponseEntity<Order> getOrderById(@PathVariable("orderId") String orderId)
      throws NotFoundException {
    Order order = storeData.get(Long.valueOf(orderId));
    if (null != order) {
      return ok(order);
    } else {
      throw new NotFoundException(404, "Order not found");
    }
  }
3. ApiParam標(biāo)記

ApiParam請求屬性,使用方式:

public ResponseEntity<User> createUser(@RequestBody @ApiParam(value = "Created user object", required = true)  User user)

與Controller中的方法并列使用媳叨。

屬性配置:

屬性名稱 備注
name 屬性名稱
value 屬性值
defaultValue 默認(rèn)屬性值
allowableValues 可以不配置
required 是否屬性必填
access 不過多描述
allowMultiple 默認(rèn)為false
hidden 隱藏該屬性
example 舉例子

在SpringMvc中的配置如下:

 public ResponseEntity<Order> getOrderById(
      @ApiParam(value = "ID of pet that needs to be fetched", allowableValues = "range[1,5]", required = true)
      @PathVariable("orderId") String orderId)
4. ApiResponse

ApiResponse:響應(yīng)配置腥光,使用方式:

@ApiResponse(code = 400, message = "Invalid user supplied")

與Controller中的方法并列使用。 屬性配置:

屬性名稱 備注
code http的狀態(tài)碼
message 描述
response 默認(rèn)響應(yīng)類 Void
reference 參考ApiOperation中配置
responseHeaders 參考 ResponseHeader 屬性配置說明
responseContainer 參考ApiOperation中配置

在SpringMvc中的配置如下:

 @RequestMapping(value = "/order", method = POST)
  @ApiOperation(value = "Place an order for a pet", response = Order.class)
  @ApiResponses({ @ApiResponse(code = 400, message = "Invalid Order") })
  public ResponseEntity<String> placeOrder(
      @ApiParam(value = "order placed for purchasing the pet", required = true) Order order) {
    storeData.add(order);
    return ok("");
  }
5. ApiResponses

ApiResponses:響應(yīng)集配置糊秆,使用方式:

 @ApiResponses({ @ApiResponse(code = 400, message = "Invalid Order") })

與Controller中的方法并列使用柴我。 屬性配置:

屬性名稱 備注
value 多個ApiResponse配置

在SpringMvc中的配置如下:

 @RequestMapping(value = "/order", method = POST)
  @ApiOperation(value = "Place an order for a pet", response = Order.class)
  @ApiResponses({ @ApiResponse(code = 400, message = "Invalid Order") })
  public ResponseEntity<String> placeOrder(
      @ApiParam(value = "order placed for purchasing the pet", required = true) Order order) {
    storeData.add(order);
    return ok("");
  }
6. ResponseHeader

響應(yīng)頭設(shè)置,使用方法

@ResponseHeader(name="head1",description="response head conf")

與Controller中的方法并列使用扩然。 屬性配置:

屬性名稱 備注
name 響應(yīng)頭名稱
description 頭描述
response 默認(rèn)響應(yīng)類 Void
responseContainer 參考ApiOperation中配置

在SpringMvc中的配置如下:

@ApiModel(description = "群組")
7. 其他
  • @ApiImplicitParams:用在方法上包含一組參數(shù)說明艘儒;
  • @ApiImplicitParam:用在@ApiImplicitParams注解中,指定一個請求參數(shù)的各個方面
    • paramType:參數(shù)放在哪個地方
    • name:參數(shù)代表的含義
    • value:參數(shù)名稱
    • dataType: 參數(shù)類型夫偶,有String/int界睁,無用
    • required : 是否必要
    • defaultValue:參數(shù)的默認(rèn)值
  • @ApiResponses:用于表示一組響應(yīng);
  • @ApiResponse:用在@ApiResponses中兵拢,一般用于表達(dá)一個錯誤的響應(yīng)信息翻斟;
    • code: 響應(yīng)碼(int型),可自定義
    • message:狀態(tài)碼對應(yīng)的響應(yīng)信息
  • @ApiModel:描述一個Model的信息(這種一般用在post創(chuàng)建的時候说铃,使用@RequestBody這樣的場景访惜,請求參數(shù)無法使用@ApiImplicitParam注解進(jìn)行描述的時候嘹履;
  • @ApiModelProperty:描述一個model的屬性。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末债热,一起剝皮案震驚了整個濱河市砾嫉,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌窒篱,老刑警劉巖焕刮,帶你破解...
    沈念sama閱讀 221,695評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異墙杯,居然都是意外死亡配并,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評論 3 399
  • 文/潘曉璐 我一進(jìn)店門高镐,熙熙樓的掌柜王于貴愁眉苦臉地迎上來溉旋,“玉大人,你說我怎么就攤上這事嫉髓」劾埃” “怎么了?”我有些...
    開封第一講書人閱讀 168,130評論 0 360
  • 文/不壞的土叔 我叫張陵岩喷,是天一觀的道長恕沫。 經(jīng)常有香客問我,道長纱意,這世上最難降的妖魔是什么婶溯? 我笑而不...
    開封第一講書人閱讀 59,648評論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘汰翠。我一直安慰自己,他們只是感情好叙身,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,655評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著硫狞,像睡著了一般信轿。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上残吩,一...
    開封第一講書人閱讀 52,268評論 1 309
  • 那天财忽,我揣著相機(jī)與錄音,去河邊找鬼泣侮。 笑死即彪,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的活尊。 我是一名探鬼主播隶校,決...
    沈念sama閱讀 40,835評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼漏益,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了深胳?” 一聲冷哼從身側(cè)響起绰疤,我...
    開封第一講書人閱讀 39,740評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎稠屠,沒想到半個月后峦睡,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體翎苫,經(jīng)...
    沈念sama閱讀 46,286評論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡权埠,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,375評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了煎谍。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片攘蔽。...
    茶點(diǎn)故事閱讀 40,505評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖呐粘,靈堂內(nèi)的尸體忽然破棺而出满俗,到底是詐尸還是另有隱情,我是刑警寧澤作岖,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布唆垃,位于F島的核電站,受9級特大地震影響痘儡,放射性物質(zhì)發(fā)生泄漏辕万。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,873評論 3 333
  • 文/蒙蒙 一沉删、第九天 我趴在偏房一處隱蔽的房頂上張望渐尿。 院中可真熱鬧,春花似錦矾瑰、人聲如沸砖茸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽凉夯。三九已至,卻和暖如春采幌,著一層夾襖步出監(jiān)牢的瞬間劲够,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評論 1 272
  • 我被黑心中介騙來泰國打工植榕, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留再沧,地道東北人。 一個月前我還...
    沈念sama閱讀 48,921評論 3 376
  • 正文 我出身青樓尊残,卻偏偏與公主長得像炒瘸,于是被迫代替她去往敵國和親淤堵。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,515評論 2 359

推薦閱讀更多精彩內(nèi)容