常用到的注解有:
- 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的屬性。