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);
}