天天看点

Spring MVC Controller参数接收方式

SpringMvc 中参数有多种接收方式:@RequestBody @RequestParam @RequestHeader @PathVariable etc.

下面详细测试,各种参数接收方式适合的场景

  1. // Content-Type application/x-www-form-urlencoded
  2. // Accept */*
  3. // Accept-Encoding gzip, deflate, br
  4. // Accept-Language zh-CN,zh;q=0.8,en;q=0.6
  5. // param1=param1&param2=param2
  6. @RequestMapping("test/testParam1")
  7. @ResponseBody
  8. public String testParam1(HttpServletRequest request, String param1, String param2){
  9. Map<String, String> result = new HashMap<String, String>();
  10. result.put("param1", param1);
  11. result.put("param2", param2);
  12. return JSON.toJSONString(result);
  13. }
  14. // Content-Type application/x-www-form-urlencoded
  15. // Accept */*
  16. // Accept-Encoding gzip, deflate, br
  17. // Accept-Language zh-CN,zh;q=0.8,en;q=0.6
  18. // param1=param1&param2=param2
  19. // 操作正常但是用RAW的方式请求,就会给出Required String parameter 'param1' is not present的异常
  20. // 原因是请求里面Content-Type: application/json 而不是 application/x-www-form-urlencoded
  21. // 参数的形式是一个json字符串,requestparam 无法解析
  22. @RequestMapping("test/testRequestParam")
  23. @ResponseBody
  24. public String testRequestParam(@RequestParam String param1, @RequestParam String param2){
  25. Map<String, String> result = new HashMap<String, String>();
  26. result.put("param1", param1);
  27. result.put("param2", param2);
  28. return JSON.toJSONString(result);
  29. }
  30. // Content-Type: application/json
  31. //如果用requestbody的形式接收,请求体里面所有的所有内容会绑定到param1中
  32. //可以看一下
  33. @RequestMapping("test/testRequestBody")
  34. @ResponseBody
  35. public String testRequestBody(@RequestBody String param1){
  36. Map<String, String> result = new HashMap<String, String>();
  37. result.put("param1", param1);
  38. return JSON.toJSONString(result);
  39. }
  40. @RequestMapping("test/testRequestbodyMap")
  41. @ResponseBody
  42. public String testRequestbodyMap(@RequestBody Map map){
  43. Map<String, Object> result = new HashMap<String, Object>();
  44. result.put("param1", map.get("param1"));
  45. return JSON.toJSONString(result);
  46. }
  47. //匹配路径,作为参数
  48. @RequestMapping("test/{param1}/{param2}")
  49. @ResponseBody
  50. public String testPathVariable(@PathVariable String param1, @PathVariable String param2){
  51. Map<String, String> result = new HashMap<String, String>();
  52. result.put("param1", param1);
  53. result.put("param2", param2);
  54. return JSON.toJSONString(result);
  55. }
  56. // param2: parameter2
  57. // param1: parameter1
  58. // Content-Type: application/json
  59. // Accept: */*
  60. // Accept-Encoding: gzip, deflate, br
  61. // Accept-Language: zh-CN,zh;q=0.8,en;q=0.6
  62. @RequestMapping("test/testRequestHeader")
  63. @ResponseBody
  64. public String testRequestHeader(@RequestHeader String param1, @RequestHeader String param2){
  65. Map<String, String> result = new HashMap<String, String>();
  66. result.put("param1", param1);
  67. result.put("param2", param2);
  68. return JSON.toJSONString(result);
  69. }
  70. // Content-Type: application/json
  71. // Accept: */*
  72. // Accept-Encoding: gzip, deflate, br
  73. // Accept-Language: zh-CN,zh;q=0.8,en;q=0.6
  74. // Cookie: JSESSIONID=1231; Hm_lvt_82116c626a8d504a5c0675073362ef6f=1491372429
  75. @RequestMapping("test/testCookieValue")
  76. @ResponseBody
  77. public String testCookieValue(@CookieValue(value = "JSESSIONID") String param1){
  78. Map<String, String> result = new HashMap<String, String>();
  79. result.put("param1", param1);
  80. return JSON.toJSONString(result);
  81. }