@ApiImplicitParam
作用在方法上,表示單獨(dú)的請(qǐng)求參數(shù)
參數(shù)
-
name
:參數(shù)名。 -
value
:參數(shù)的具體意義,作用。 -
required
:參數(shù)是否必填。 -
dataType
:參數(shù)的數(shù)據(jù)類(lèi)型。 -
paramType
:查詢(xún)參數(shù)類(lèi)型,這里有幾種形式:
類(lèi)型 作用
-
path
以地址的形式提交數(shù)據(jù) -
query
直接跟參數(shù)完成自動(dòng)映射賦值 -
body
以流的形式提交 僅支持POST -
header
參數(shù)在request headers 里邊提交 -
form
以form表單的形式提交 僅支持POST
在這里我被坑過(guò)一次:當(dāng)我發(fā)POST請(qǐng)求的時(shí)候,當(dāng)時(shí)接受的整個(gè)參數(shù),不論我用body還是query,后臺(tái)都會(huì)報(bào)Body Missing錯(cuò)誤。
這個(gè)參數(shù)和SpringMvc中的@RequestBody沖突,索性我就去掉了paramType,對(duì)接口測(cè)試并沒(méi)有影響。
@ApiImplicitParams
用于方法,包含多個(gè) @ApiImplicitParam:
例:
1
2
3
4
5
6
7
8
|
@ApiOperation ( "查詢(xún)測(cè)試" ) @GetMapping ( "select" ) //@ApiImplicitParam(name="name",value="用戶(hù)名",dataType="String", paramType = "query") @ApiImplicitParams ({ @ApiImplicitParam (name= "name" ,value= "用戶(hù)名" ,dataType= "string" , paramType = "query" ,example= "xingguo" ), @ApiImplicitParam (name= "id" ,value= "用戶(hù)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 = "信息參數(shù)" , required = true ) }) @RequestMapping (value = "/findById3" , method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE) @RequestBody MessageParam param |
提交的參數(shù)是這個(gè)對(duì)象的一個(gè)json,然后會(huì)自動(dòng)解析到對(duì)應(yīng)的字段上去,也可以通過(guò)流的形式接收當(dāng)前的請(qǐng)求數(shù)據(jù),但是這個(gè)和上面的接收方式僅能使用一個(gè)(用@RequestBody之后流就會(huì)關(guān)閉了)
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) |
小結(jié)一下
(1)對(duì)于@ApiImplicitParam的paramType:query、form域中的值需要使用@RequestParam獲取, header域中的值需要使用@RequestHeader來(lái)獲取,path域中的值需要使用@PathVariable來(lái)獲取,body域中的值使用@RequestBody來(lái)獲取,否則可能出錯(cuò);而且如果paramType是body,name就不能是body,否則有問(wèn)題,與官方文檔中的“If paramType is "body", the name should be "body"不符。
-
@ApiImplicitParams
:用在方法上包含一組參數(shù)說(shuō)明 -
@ApiImplicitParam
:用在@ApiImplicitParams注解中,指定一個(gè)請(qǐng)求參數(shù)的各個(gè)方面 -
paramType
:參數(shù)放在哪個(gè)地方 -
header
-->請(qǐng)求參數(shù)的獲取:@RequestHeader -
query
-->請(qǐng)求參數(shù)的獲取:@RequestParam -
path
(用于restful接口)-->請(qǐng)求參數(shù)的獲取:@PathVariable -
body
(不常用) -
form
(不常用) -
name
:參數(shù)名 -
dataType
:參數(shù)類(lèi)型 -
required
:參數(shù)是否必須傳 -
value
:參數(shù)的意思 -
defaultValue
:參數(shù)的默認(rèn)值 -
@ApiResponses
:用于表示一組響應(yīng) -
@ApiResponse
:用在@ApiResponses中,一般用于表達(dá)一個(gè)錯(cuò)誤的響應(yīng)信息 -
code
:數(shù)字,例如400 -
message
:信息,例如"請(qǐng)求參數(shù)沒(méi)填好" -
response
:拋出異常的類(lèi) - 以上這些就是最常用的幾個(gè)注解了。
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相關(guān)api" ) public class UserController { @Autowired private UserService userService; @ApiOperation ( "獲取用戶(hù)信息" ) @ApiImplicitParams ({ @ApiImplicitParam (paramType= "header" ,name= "username" ,dataType= "String" ,required= true ,value= "用戶(hù)的姓名" ,defaultValue= "zhaojigang" ), @ApiImplicitParam (paramType= "query" ,name= "password" ,dataType= "String" ,required= true ,value= "用戶(hù)的密碼" ,defaultValue= "wangna" ) }) @ApiResponses ({ @ApiResponse (code= 400 ,message= "請(qǐng)求參數(shù)沒(méi)填好" ), @ApiResponse (code= 404 ,message= "請(qǐng)求路徑?jīng)]有或頁(yè)面跳轉(zhuǎn)路徑不對(duì)" ) }) @RequestMapping (value= "/getUser" ,method=RequestMethod.GET) public User getUser( @RequestHeader ( "username" ) String username, @RequestParam ( "password" ) String password) { return userService.getUser(username,password); } } |
測(cè)試
啟動(dòng)服務(wù),瀏覽器輸入"http://localhost:8080/swagger-ui.html"
在上面案例中我們可以知道如果在request域中我們使用reques.getHeader()和使用@RequestHeader注解作用是一樣的,其它內(nèi)容類(lèi)似。
-
@ApiResponses
:用于表示一組響應(yīng) -
@ApiResponse
:用在@ApiResponses中,一般用于表達(dá)一個(gè)錯(cuò)誤的響應(yīng)信息 -
code
:數(shù)字,例如400 -
message
:信息,例如”請(qǐng)求參數(shù)沒(méi)填好” -
response
:拋出異常的類(lèi)
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@ApiOperation ( "獲取用戶(hù)信息" ) @ApiImplicitParams ({ @ApiImplicitParam (paramType= "header" ,name= "name" ,dataType= "String" ,required= true ,value= "用戶(hù)的姓名" ,defaultValue= "zhaojigang" ), @ApiImplicitParam (paramType= "query" ,name= "pwd" ,dataType= "String" ,required= true ,value= "用戶(hù)的密碼" ,defaultValue= "wangna" ) }) @ApiResponses ({ @ApiResponse (code= 400 ,message= "請(qǐng)求參數(shù)沒(méi)填好" ), @ApiResponse (code= 404 ,message= "請(qǐng)求路徑?jīng)]有或頁(yè)面跳轉(zhuǎn)路徑不對(duì)" ) }) @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); } |
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://www.cnblogs.com/h-c-g/p/11004020.html