@[email protected] @Path
一、問題描述
由於專案是前後端分離,因此後臺使用的是spring boot,做成微服務,只暴露介面。介面設計風格為restful的風格,在get請求下,後臺接收引數的註解為RequestBody時會報錯;在post請求下,後臺接收引數的註解為RequestParam時也會報錯。
二、問題原因
由於spring的RequestParam註解接收的引數是來自於requestHeader中,即請求頭,也就是在url中,格式為xxx?username=123&password=456,而RequestBody註解接收的引數則是來自於requestBody中,即請求體中。
三、解決方法
因此綜上所述,如果為get請求時,後臺接收引數的註解應該為RequestParam,如果為post請求時,則後臺接收引數的註解就是為RequestBody。附上兩個例子,截圖如下:
get請求
post請求
另外,還有一種應用場景,介面規範為resultful風格時,舉個例子:如果要獲取某個id下此條問題答案的查詢次數的話,則後臺就需要動態獲取引數,其註解為@PathVariable,並且requestMapping中的value應為value="/{id}/queryNum",截圖如下:
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@ApiParam @PathVariable @RequestParam三者區別
@ApiOperation( value = "編輯公告", notes = "編輯公告", httpMethod = "POST" ) @RequestMapping( value = "/edit", method = RequestMethod.POST ) public RequestResult edit( @ApiParam(name = "title", value = "公告標題", required = true) @RequestParam("title") String title, @ApiParam(name = "content", value = "公告內容", required = true) @RequestParam("content") String content){
[email protected],是獲取前端傳遞給後端的引數,可以是get方式,也可以是post方式。其中如果前端傳遞的引數和後端你接受的引數起的名字欄位是一致的可以省略不寫,也可以直接寫@RequestParam String title,如果不一致一定要完整寫,不然獲取不到,如下面的bis_key就必須寫。(必須是 ?key=value&key=value的URL中)
@ApiOperation( value = "編輯公告", notes = "編輯公告", httpMethod = "POST" ) @RequestMapping( value = "/edit", method = RequestMethod.POST ) public RequestResult edit( @ApiParam(name = "bis_key", value = "bis_key", required = true) String bisKey, @ApiParam(name = "title", value = "公告標題", required = true) @RequestParam String title, @ApiParam(name = "content", value = "公告內容", required = true) String content,
[email protected],是獲取get方式,url後面引數,進行引數繫結
@ApiOperation(value = "刪除公告", notes = "刪除公告", httpMethod = "POST") @RequestMapping(value = "/delete/{bisKey}", method = RequestMethod.POST) public RequestResult remove(@ApiParam(name = "bisKey", value = "需要刪除的公告ids", required = true) @PathVariable String bisKey) {
[email protected] 顧名思義,是註解api的引數,也就是用於swagger提供開發者文件,文件中生成的註釋內容。
@ApiOperation( value = "編輯公告", notes = "編輯公告", httpMethod = "POST" ) @RequestMapping( value = "/edit", method = RequestMethod.POST ) public RequestResult edit( @ApiParam(name = "title", value = "公告標題", required = true) @RequestParam("title") String title, @ApiParam(name = "content", value = "公告內容", required = true) @RequestParam("content") String content){
[email protected],是獲取前端傳遞給後端的引數,可以是get方式,也可以是post方式。其中如果前端傳遞的引數和後端你接受的引數起的名字欄位是一致的可以省略不寫,也可以直接寫@RequestParam String title,如果不一致一定要完整寫,不然獲取不到,如下面的bis_key就必須寫。(必須是 ?key=value&key=value的URL中)
@ApiOperation( value = "編輯公告", notes = "編輯公告", httpMethod = "POST" ) @RequestMapping( value = "/edit", method = RequestMethod.POST ) public RequestResult edit( @ApiParam(name = "bis_key", value = "bis_key", required = true) String bisKey, @ApiParam(name = "title", value = "公告標題", required = true) @RequestParam String title, @ApiParam(name = "content", value = "公告內容", required = true) String content,
[email protected],是獲取get方式,url後面引數,進行引數繫結
@ApiOperation(value = "刪除公告", notes = "刪除公告", httpMethod = "POST") @RequestMapping(value = "/delete/{bisKey}", method = RequestMethod.POST) public RequestResult remove(@ApiParam(name = "bisKey", value = "需要刪除的公告ids", required = true) @PathVariable String bisKey) {