@ApiImplicitParam
作用在方法上,表示單獨的請求參數
參數
-
name
:參數名。 -
value
:參數的具體意義,作用。 -
required
:參數是否必填。 -
dataType
:參數的數據類型。 -
paramType
:查詢參數類型,這里有幾種形式:
類型 作用
-
path
以地址的形式提交數據 -
query
直接跟參數完成自動映射賦值 -
body
以流的形式提交 僅支持POST -
header
參數在request headers 里邊提交 -
form
以form表單的形式提交 僅支持POST
在這里我被坑過一次:當我發POST請求的時候,當時接受的整個參數,不論我用body還是query,后臺都會報Body Missing錯誤。
這個參數和SpringMvc中的@RequestBody沖突,索性我就去掉了paramType,對接口測試并沒有影響。
@ApiImplicitParams
用于方法,包含多個 @ApiImplicitParam:
例:
1
2
3
4
5
6
7
8
|
@ApiOperation ( "查詢測試" ) @GetMapping ( "select" ) //@ApiImplicitParam(name="name",value="用戶名",dataType="String", paramType = "query") @ApiImplicitParams ({ @ApiImplicitParam (name= "name" ,value= "用戶名" ,dataType= "string" , paramType = "query" ,example= "xingguo" ), @ApiImplicitParam (name= "id" ,value= "用戶id" ,dataType= "long" , paramType = "query" )}) public void select(){ } |
paramType 示例詳解
path
1
2
|
@RequestMapping (value = "/findById1/{id}" , method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) @PathVariable (name = "id" ) Long id |
body
1
2
3
|
@ApiImplicitParams ({ @ApiImplicitParam (paramType = "body" , dataType = "MessageParam" , name = "param" , value = "信息參數" , required = true ) }) @RequestMapping (value = "/findById3" , method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE) @RequestBody MessageParam param |
提交的參數是這個對象的一個json,然后會自動解析到對應的字段上去,也可以通過流的形式接收當前的請求數據,但是這個和上面的接收方式僅能使用一個(用@RequestBody之后流就會關閉了)
header
1
2
3
4
5
|
@ApiImplicitParams ({ @ApiImplicitParam (paramType = "header" , dataType = "Long" , name = "id" , value = "信息id" , required = true ) }) String idstr = request.getHeader( "id" ); if (StringUtils.isNumeric(idstr)) { id = Long.parseLong(idstr); } |
Form
1
2
|
@ApiImplicitParams ({ @ApiImplicitParam (paramType = "form" , dataType = "Long" , name = "id" , value = "信息id" , required = true ) }) @RequestMapping (value = "/findById5" , method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) |
小結一下
(1)對于@ApiImplicitParam的paramType:query、form域中的值需要使用@RequestParam獲取, header域中的值需要使用@RequestHeader來獲取,path域中的值需要使用@PathVariable來獲取,body域中的值使用@RequestBody來獲取,否則可能出錯;而且如果paramType是body,name就不能是body,否則有問題,與官方文檔中的“If paramType is "body", the name should be "body"不符。
-
@ApiImplicitParams
:用在方法上包含一組參數說明 -
@ApiImplicitParam
:用在@ApiImplicitParams注解中,指定一個請求參數的各個方面 -
paramType
:參數放在哪個地方 -
header
-->請求參數的獲取:@RequestHeader -
query
-->請求參數的獲取:@RequestParam -
path
(用于restful接口)-->請求參數的獲?。篅PathVariable -
body
(不常用) -
form
(不常用) -
name
:參數名 -
dataType
:參數類型 -
required
:參數是否必須傳 -
value
:參數的意思 -
defaultValue
:參數的默認值 -
@ApiResponses
:用于表示一組響應 -
@ApiResponse
:用在@ApiResponses中,一般用于表達一個錯誤的響應信息 -
code
:數字,例如400 -
message
:信息,例如"請求參數沒填好" -
response
:拋出異常的類 - 以上這些就是最常用的幾個注解了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiResponse; import io.swagger.annotations.ApiResponses; @RestController @RequestMapping ( "/user" ) @Api ( "userController相關api" ) public class UserController { @Autowired private UserService userService; @ApiOperation ( "獲取用戶信息" ) @ApiImplicitParams ({ @ApiImplicitParam (paramType= "header" ,name= "username" ,dataType= "String" ,required= true ,value= "用戶的姓名" ,defaultValue= "zhaojigang" ), @ApiImplicitParam (paramType= "query" ,name= "password" ,dataType= "String" ,required= true ,value= "用戶的密碼" ,defaultValue= "wangna" ) }) @ApiResponses ({ @ApiResponse (code= 400 ,message= "請求參數沒填好" ), @ApiResponse (code= 404 ,message= "請求路徑沒有或頁面跳轉路徑不對" ) }) @RequestMapping (value= "/getUser" ,method=RequestMethod.GET) public User getUser( @RequestHeader ( "username" ) String username, @RequestParam ( "password" ) String password) { return userService.getUser(username,password); } } |
測試
啟動服務,瀏覽器輸入"http://localhost:8080/swagger-ui.html"
在上面案例中我們可以知道如果在request域中我們使用reques.getHeader()和使用@RequestHeader注解作用是一樣的,其它內容類似。
-
@ApiResponses
:用于表示一組響應 -
@ApiResponse
:用在@ApiResponses中,一般用于表達一個錯誤的響應信息 -
code
:數字,例如400 -
message
:信息,例如”請求參數沒填好” -
response
:拋出異常的類
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@ApiOperation ( "獲取用戶信息" ) @ApiImplicitParams ({ @ApiImplicitParam (paramType= "header" ,name= "name" ,dataType= "String" ,required= true ,value= "用戶的姓名" ,defaultValue= "zhaojigang" ), @ApiImplicitParam (paramType= "query" ,name= "pwd" ,dataType= "String" ,required= true ,value= "用戶的密碼" ,defaultValue= "wangna" ) }) @ApiResponses ({ @ApiResponse (code= 400 ,message= "請求參數沒填好" ), @ApiResponse (code= 404 ,message= "請求路徑沒有或頁面跳轉路徑不對" ) }) @RequestMapping (value= "/getUser" ,method= RequestMethod.GET) public User getUser( @RequestHeader ( "name" ) String name, @RequestParam ( "pwd" ) String pwd) { System.out.println(name); System.out.println(pwd); return userRepository.getUserByNameAndPwd(name,pwd); } |
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://www.cnblogs.com/h-c-g/p/11004020.html