Spring MVC Controller参数接收方式

6 篇文章 0 订阅

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. }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring MVC是一种基于MVC(Model-View-Controller)设计模式的Web框架,它可以帮助我们快速地开发Web应用程序。在Spring MVC中,Controller是控制器的核心组件,它负责接收用户请求并决定如何处理这些请求。 在Spring MVC中,我们可以通过编写Controller类来实现请求的处理。在Controller类中,我们可以定义多个方法,每个方法对应处理一个具体的请求。这些方法通常使用注解来标识它们应该处理哪些请求。 例如,我们可以使用@Controller注解来标识一个类为Controller,并使用@RequestMapping注解来标识一个方法应该处理哪些请求。下面是一个简单的Controller类的示例: ``` @Controller @RequestMapping("/hello") public class HelloController { @RequestMapping("/world") public ModelAndView helloWorld() { String message = "Hello World, Spring MVC!"; return new ModelAndView("hello", "message", message); } } ``` 在上面的示例中,我们使用@Controller注解将HelloController类标识为Controller,并使用@RequestMapping注解将该类处理的请求路径设置为“/hello”。我们还使用@RequestMapping注解将helloWorld方法标识为处理“/hello/world”请求的方法。该方法返回一个包含“Hello World, Spring MVC!”消息的ModelAndView对象。 需要注意的是,Controller类的方法可以返回不同类型的结果。除了ModelAndView之外,还可以返回String、void、HttpEntity、ResponseEntity等类型的结果。这些结果将由Spring MVC框架进行处理,并将相应的内容返回给客户端。 总的来说,Spring MVCController是Web应用程序的核心组件之一,它可以帮助我们快速地开发功能强大、易于维护的Web应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值