關(guān)于@ApiImplicitParams、ApiImplicitParam的使用說明
@ApiImplicitParam
作用在方法上,表示單獨的請求參數(shù)
參數(shù)
name:參數(shù)名。value:參數(shù)的具體意義,作用。required:參數(shù)是否必填。dataType:參數(shù)的數(shù)據(jù)類型。paramType:查詢參數(shù)類型,這里有幾種形式:
類型 作用
path以地址的形式提交數(shù)據(jù)query直接跟參數(shù)完成自動映射賦值body以流的形式提交 僅支持POSTheader參數(shù)在request headers 里邊提交form以form表單的形式提交 僅支持POST
在這里我被坑過一次:當(dāng)我發(fā)POST請求的時候,當(dāng)時接受的整個參數(shù),不論我用body還是query,后臺都會報Body Missing錯誤。
這個參數(shù)和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 = "信息參數(shù)", required = true) })
@RequestMapping(value = "/findById3", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
@RequestBody MessageParam param
提交的參數(shù)是這個對象的一個json,然后會自動解析到對應(yīng)的字段上去,也可以通過流的形式接收當(dāng)前的請求數(shù)據(jù),但是這個和上面的接收方式僅能使用一個(用@RequestBody之后流就會關(guān)閉了)
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)
小結(jié)一下
(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:用在方法上包含一組參數(shù)說明@ApiImplicitParam:用在@ApiImplicitParams注解中,指定一個請求參數(shù)的各個方面paramType:參數(shù)放在哪個地方header-->請求參數(shù)的獲?。篅RequestHeaderquery-->請求參數(shù)的獲?。篅RequestParampath(用于restful接口)-->請求參數(shù)的獲?。篅PathVariablebody(不常用)form(不常用)name:參數(shù)名dataType:參數(shù)類型required:參數(shù)是否必須傳value:參數(shù)的意思defaultValue:參數(shù)的默認(rèn)值@ApiResponses:用于表示一組響應(yīng)@ApiResponse:用在@ApiResponses中,一般用于表達(dá)一個錯誤的響應(yīng)信息code:數(shù)字,例如400message:信息,例如"請求參數(shù)沒填好"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相關(guān)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="請求參數(shù)沒填好"),
@ApiResponse(code=404,message="請求路徑?jīng)]有或頁面跳轉(zhuǎn)路徑不對")
})
@RequestMapping(value="/getUser",method=RequestMethod.GET)
public User getUser(@RequestHeader("username") String username, @RequestParam("password") String password) {
return userService.getUser(username,password);
}
}
測試
啟動服務(wù),瀏覽器輸入"http://localhost:8080/swagger-ui.html"

在上面案例中我們可以知道如果在request域中我們使用reques.getHeader()和使用@RequestHeader注解作用是一樣的,其它內(nèi)容類似。
@ApiResponses:用于表示一組響應(yīng)@ApiResponse:用在@ApiResponses中,一般用于表達(dá)一個錯誤的響應(yīng)信息code:數(shù)字,例如400message:信息,例如”請求參數(shù)沒填好”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="請求參數(shù)沒填好"),
@ApiResponse(code=404,message="請求路徑?jīng)]有或頁面跳轉(zhuǎn)路徑不對")
})
@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);
}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot集成Zipkin實現(xiàn)分布式全鏈路監(jiān)控
這篇文章主要介紹了SpringBoot集成Zipkin實現(xiàn)分布式全鏈路監(jiān)控的方法啊,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-09-09
SpringBoot使用MockMvc測試get和post接口的示例代碼
Spring Boot MockMvc是一個用于單元測試的模塊,它是Spring框架的一部分,專注于簡化Web應(yīng)用程序的測試,MockMvc主要用來模擬一個完整的HTTP請求-響應(yīng)生命周期,本文給大家介紹了SpringBoot使用MockMvc測試get和post接口,需要的朋友可以參考下2024-06-06
Mybatis中SqlSession下的四大對象之執(zhí)行器(executor)
mybatis中sqlsession下的四大對象是指:executor, statementHandler,parameterHandler,resultHandler對象。這篇文章主要介紹了Mybatis中SqlSession下的四大對象之執(zhí)行器(executor),需要的朋友可以參考下2019-04-04
Java基礎(chǔ)篇之serialVersionUID用法及注意事項詳解
這篇文章主要給大家介紹了關(guān)于Java基礎(chǔ)篇之serialVersionUID用法及注意事項的相關(guān)資料,SerialVersionUID屬性是用于序列化/反序列化可序列化類的對象的標(biāo)識符,我們可以用它來記住可序列化類的版本,以驗證加載的類和序列化對象是否兼容,需要的朋友可以參考下2024-02-02
java后臺發(fā)起get請求獲取響應(yīng)數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了java后臺發(fā)起get請求獲取響應(yīng)數(shù)據(jù),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-08-08
優(yōu)化SpringBoot程序啟動速度的實現(xiàn)
本文主要介紹了優(yōu)化SpringBoot程序啟動速度的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
java Spring松耦合高效應(yīng)用簡單實例分析
在Java項目,龐大的對象依賴關(guān)系將一直緊密耦合引起對象難以管理或修改。在這種情況下,可以使用Spring框架作為一個核心模塊輕松高效地管理所有的對象依賴。本文章向大家介紹Spring松耦合的實例,需要的朋友可以參考一下。2016-12-12

