SpringMvc 中參數有多種接收方式:@RequestBody @RequestParam @RequestHeader @PathVariable etc.
下面詳細測試,各種參數接收方式適合的場景
// Content-Type application/x-www-form-urlencoded
// Accept */*
// Accept-Encoding gzip, deflate, br
// Accept-Language zh-CN,zh;q=0.8,en;q=0.6
// param1=param1¶m2=param2
@RequestMapping("test/testParam1")
@ResponseBody
public String testParam1(HttpServletRequest request, String param1, String param2){
Map<String, String> result = new HashMap<String, String>();
result.put("param1", param1);
result.put("param2", param2);
return JSON.toJSONString(result);
}
// Content-Type application/x-www-form-urlencoded
// Accept */*
// Accept-Encoding gzip, deflate, br
// Accept-Language zh-CN,zh;q=0.8,en;q=0.6
// param1=param1¶m2=param2
// 操作正常但是用RAW的方式請求,就會給出Required String parameter 'param1' is not present的異常
// 原因是請求裏面Content-Type: application/json 而不是 application/x-www-form-urlencoded
// 參數的形式是一個json字符串,requestparam 無法解析
@RequestMapping("test/testRequestParam")
@ResponseBody
public String testRequestParam(@RequestParam String param1, @RequestParam String param2){
Map<String, String> result = new HashMap<String, String>();
result.put("param1", param1);
result.put("param2", param2);
return JSON.toJSONString(result);
}
// Content-Type: application/json
//如果用requestbody的形式接收,請求體裏面所有的所有內容會綁定到param1中
//可以看一下
@RequestMapping("test/testRequestBody")
@ResponseBody
public String testRequestBody(@RequestBody String param1){
Map<String, String> result = new HashMap<String, String>();
result.put("param1", param1);
return JSON.toJSONString(result);
}
@RequestMapping("test/testRequestbodyMap")
@ResponseBody
public String testRequestbodyMap(@RequestBody Map map){
Map<String, Object> result = new HashMap<String, Object>();
result.put("param1", map.get("param1"));
return JSON.toJSONString(result);
}
//匹配路徑,作爲參數
@RequestMapping("test/{param1}/{param2}")
@ResponseBody
public String testPathVariable(@PathVariable String param1, @PathVariable String param2){
Map<String, String> result = new HashMap<String, String>();
result.put("param1", param1);
result.put("param2", param2);
return JSON.toJSONString(result);
}
// param2: parameter2
// param1: parameter1
// Content-Type: application/json
// Accept: */*
// Accept-Encoding: gzip, deflate, br
// Accept-Language: zh-CN,zh;q=0.8,en;q=0.6
@RequestMapping("test/testRequestHeader")
@ResponseBody
public String testRequestHeader(@RequestHeader String param1, @RequestHeader String param2){
Map<String, String> result = new HashMap<String, String>();
result.put("param1", param1);
result.put("param2", param2);
return JSON.toJSONString(result);
}
// Content-Type: application/json
// Accept: */*
// Accept-Encoding: gzip, deflate, br
// Accept-Language: zh-CN,zh;q=0.8,en;q=0.6
// Cookie: JSESSIONID=1231; Hm_lvt_82116c626a8d504a5c0675073362ef6f=1491372429
@RequestMapping("test/testCookieValue")
@ResponseBody
public String testCookieValue(@CookieValue(value = "JSESSIONID") String param1){
Map<String, String> result = new HashMap<String, String>();
result.put("param1", param1);
return JSON.toJSONString(result);
}