前言
在開發過程中,後臺難免會接收到各種各樣的參數類型,比如對象,單個參數,List集合類型,數組類型,等等,如果你是後端開發者,在沒有前端接口請求的情況下,該怎麼測試呢?
一、接受List類型的複雜參數
1、列子一、只接受一個List類型:
普通的@RequestParam接受參數
@PostMapping("/updateProductStatus")
public JSONObject updateProductStatus(@RequestParam(value = "ids",required = false) List<Integer> ids){
JSONObject json = new JSONObject();
json.put("code",200);
json.put("message","SUCCESS");
json.put("ids",ids);
return json;
}
Postman測試接口:
第一種寫法:
第二種寫法:
1.1、列子一、接受一個List類型和Integer類型參數:
public JSONObject updateProductStatus(@RequestParam(value = "ids",required = false) List<Integer> ids,
@RequestParam(value = "status",required = false) Integer status)
Postman測試接口:
2、列子二:
@RequestBody接受前端傳來的json數據
public JSONObject updateProductStatus1(@RequestBody List<Integer> ids)
Postman測試接口:
選擇Body -》raw-》JSON
1.1、列子二、接受一個List類型和Integer類型參數:
@PostMapping("/updateProductStatus2")
public JSONObject updateProductStatus2(@RequestBody HashMap<String,Object> map){
JSONObject json = new JSONObject();
// 接收List
List<Integer> ids = (List<Integer>) map.get("ids");
// 接收另外一個參數
Integer status = (Integer) map.get("status");
// 返回
json.put("code",200);
json.put("message","SUCCESS");
json.put("ids",ids);
json.put("status",status);
return json;
}
Postman測試接口:
普通的和json參數發送使用Postman做測試先這樣,其他的類型的後續會繼續發文章