關于@ApiImplicitParams、ApiImplicitParam的使用說明
@ApiImplicitParam
作用在方法上,表示單獨的請求參數
參數
name:參數名。value:參數的具體意義,作用。required:參數是否必填。dataType:參數的數據類型。paramType:查詢參數類型,這里有幾種形式:
類型 作用
path以地址的形式提交數據query直接跟參數完成自動映射賦值body以流的形式提交 僅支持POSTheader參數在request headers 里邊提交form以form表單的形式提交 僅支持POST
在這里我被坑過一次:當我發(fā)POST請求的時候,當時接受的整個參數,不論我用body還是query,后臺都會報Body Missing錯誤。
這個參數和SpringMvc中的@RequestBody沖突,索性我就去掉了paramType,對接口測試并沒有影響。
@ApiImplicitParams
用于方法,包含多個 @ApiImplicitParam:
例:
@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
@RequestMapping(value = "/findById1/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@PathVariable(name = "id") Long id
body
@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
@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
@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-->請求參數的獲取:@RequestHeaderquery-->請求參數的獲?。篅RequestParampath(用于restful接口)-->請求參數的獲取:@PathVariablebody(不常用)form(不常用)name:參數名dataType:參數類型required:參數是否必須傳value:參數的意思defaultValue:參數的默認值@ApiResponses:用于表示一組響應@ApiResponse:用在@ApiResponses中,一般用于表達一個錯誤的響應信息code:數字,例如400message:信息,例如"請求參數沒填好"response:拋出異常的類- 以上這些就是最常用的幾個注解了。
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:數字,例如400message:信息,例如”請求參數沒填好”response:拋出異常的類
@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);
}
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
在Spring中使用Knife4j進行API文檔生成與管理的操作方法
Knife4j 是為Java MVC 框架(如Spring Boot、Spring MVC等)集成 Swagger 生成 API 文檔的增強解決方案,它基于 Swagger 的核心功能,通過定制化的前端界面和一些額外的特性,本文介紹了在Spring中使用Knife4j進行API文檔生成與管理的操作方法,需要的朋友可以參考下2024-12-12
記一次Maven項目改造成SpringBoot項目的過程實踐
本文主要介紹了Maven項目改造成SpringBoot項目的過程實踐,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03

