引子
我們想測試如下接口
@RequestMapping(value ="saveMessageRecover", method = RequestMethod.POST)
@ResponseBody
public AjaxMessage saveMessageRecover(@RequestBody List<Device> deviceList,
@RequestParam("eventId") Long eventId,
@RequestParam("tagValue") String tagValue) {
System.out.println(deviceList.toString());
return emergencyDeviceRecoverService.saveMessageRecover(deviceList, eventId, tagValue);
}
如何測試以及遇到的問題
@RequestMapping(value ="saveMessageRecover", method = RequestMethod.POST)
@ResponseBody
public AjaxMessage save(@RequestBody List<Device> deviceList,
@RequestParam("eventId") Long eventId,
@RequestParam("tagValue") String tagValue) {
return emergencyDeviceRecoverService.saveMessageRecover(list, eventId, tagValue);
}
這里對應(yīng)了三個傳入?yún)?shù),因為deviceList是必須的,Device作為我們自己封裝的JavaBean,我們想要直接傳參的話,前端傳遞一個List過來,這個是被@Requestbody修飾的,同時還存在兩個由@RequestParam修飾的包裝類,因此,
@RequestBody主要用來接收前端傳遞給后端的json字符串中的數(shù)據(jù)的(請求體中的數(shù)據(jù)的);而最常用的使用請求體傳參的無疑是POST請求了,所以使用@RequestBody接收數(shù)據(jù)時,一般都用POST方式進(jìn)行提交。在后端的同一個接收方法里,@RequestBody與@RequestParam()可以同時使用,@RequestBody最多只能有一個,而@RequestParam()可以有多個。
-
為什么要使用@RequestBody,直接傳不行嗎?
當(dāng)前端傳來的是一個完整對象的時候,而且是以
json
格式傳輸,只有加上注解@RequestBody
,Spring 才會自動將 JSON 類型數(shù)據(jù)與我們的類進(jìn)行匹配 -
返回給前端的是否為JSON類型?
使用@RestController注解或者@Controller+@Responbody注解
- postman如何傳入list類型的參數(shù)?
我們遇到的問題?
- 一開始總是使用大括號包圍這個Bean,實際上傳入一個list不需要如此。
- 不確定如何傳入一個@RequestBody和多個@RequestParam時,如何使用postman測試
如果想直接傳一個list的對象集合,則不需要大括號;
如果是
public JSONObject updateProductStatus(@RequestParam(value = "ids",required = false) List<Integer> ids){
參考文獻(xiàn)
-
postman入門學(xué)習(xí)
鏈接: postman入門學(xué)習(xí) -
SpringMVC參數(shù)綁定學(xué)習(xí)總結(jié)【前后端數(shù)據(jù)參數(shù)傳遞】
鏈接: SpringMVC -
@RequestBody的使用
鏈接: RequestBody -
@RequestBody和@RequestParam區(qū)別全面詳細(xì)
鏈接: RequestParam -
Postman測試接口傳入List類型的參數(shù)以及數(shù)組類型參數(shù)
鏈接: Postman文章來源:http://www.zghlxwxcb.cn/news/detail-568302.html -
前后端交互--------后端傳JSON給前端
鏈接: JSON文章來源地址http://www.zghlxwxcb.cn/news/detail-568302.html
到了這里,關(guān)于Postman傳遞@requestbody標(biāo)注的List集合的傳參遇到的問題的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!