Api 用在類上,說明該類的做用。能夠標記一個Controller類作爲swagger 文檔資源,使用方式:css
@Api(value = "/user", description = "Operations about user")
與Controller註解並列使用。 屬性配置:java
屬性名稱 | 備註 |
---|---|
value | url的路徑值 |
tags | 若是設置這個值、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 | 高級特性認證時配置 |
hidden | 配置爲true 將在文檔中隱藏 |
在SpringMvc中的配置以下:json
@Controller @RequestMapping(value = "/api/pet", produces = {APPLICATION_JSON_VALUE, APPLICATION_XML_VALUE}) @Api(value = "/pet", description = "Operations about pets") public class PetController { }
ApiOperation:用在方法上,說明方法的做用,每個url資源的定義,使用方式:api
@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中的方法並列使用。
屬性配置:ruby
屬性名稱 | 備註 |
---|---|
value | url的路徑值 |
tags | 若是設置這個值、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 | 高級特性認證時配置 |
hidden | 配置爲true 將在文檔中隱藏 |
response | 返回的對象 |
responseContainer | 這些對象是有效的 "List", "Set" or "Map".,其餘無效 |
httpMethod | "GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS" and "PATCH" |
code | http的狀態碼 默認 200 |
extensions | 擴展屬性 |
在SpringMvc中的配置以下:app
@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"); } }
ApiParam請求屬性,使用方式:post
public ResponseEntity<User> createUser(@RequestBody @ApiParam(value = "Created user object", required = true) User user)
與Controller中的方法並列使用。fetch
屬性配置:ui
屬性名稱 | 備註 |
---|---|
name | 屬性名稱 |
value | 屬性值 |
defaultValue | 默認屬性值 |
allowableValues | 能夠不配置 |
required | 是否屬性必填 |
access | 不過多描述 |
allowMultiple | 默認爲false |
hidden | 隱藏該屬性 |
example | 舉例子 |
在SpringMvc中的配置以下:url
public ResponseEntity<Order> getOrderById(
@ApiParam(value = "ID of pet that needs to be fetched", allowableValues = "range[1,5]", required = true) @PathVariable("orderId") String orderId)
ApiResponse:響應配置,使用方式:
@ApiResponse(code = 400, message = "Invalid user supplied")
與Controller中的方法並列使用。 屬性配置:
屬性名稱 | 備註 |
---|---|
code | http的狀態碼 |
message | 描述 |
response | 默認響應類 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(""); }
ApiResponses:響應集配置,使用方式:
@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(""); }
響應頭設置,使用方法
@ResponseHeader(name="head1",description="response head conf")
與Controller中的方法並列使用。 屬性配置:
屬性名稱 | 備註 |
---|---|
name | 響應頭名稱 |
description | 頭描述 |
response | 默認響應類 Void |
responseContainer | 參考ApiOperation中配置 |
在SpringMvc中的配置以下:
@ApiModel(description = "羣組")