经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Java » 查看文章
Spring接收web请求参数的几种方式
来源:cnblogs  作者:lasdaybg  时间:2018/10/26 10:14:15  对本文有异议

1 查询参数

请求格式:url?参数1=值1&参数2=值2...
同时适用于GET和POST方式
spring处理查询参数的方法又有几种写法:

方法一:
方法参数名即为请求参数名

  1. // 查询参数1
  2. @RequestMapping(value = "/test/query1", method = RequestMethod.GET)
  3. public String testQuery1(String username, String password) {
  4. System.out.println("username=" + username + ", password=" + password);
  5. return "username=" + username + ", password=" + password;
  6. }

 

方法二:
从HttpServletRequest中提取参数

  1. // 查询参数2
  2. @RequestMapping(value = "/test/query2", method = RequestMethod.GET)
  3. public String testQuery2(HttpServletRequest request) {
  4. String username = request.getParameter("username");
  5. String password = request.getParameter("password");
  6. System.out.println("username=" + username + ", password=" + password);
  7. return "username=" + username + ", password=" + password;
  8. }


方法三:
方法参数名和请求参数名可以不一样,通过@RequestParam注解来绑定参数

  1. // 查询参数3
  2. @RequestMapping(value = "/test/query3", method = RequestMethod.GET)
  3. public String testQuery3(@RequestParam("username") String un, @RequestParam("password") String pw) {
  4. System.out.println("username=" + un + ", password=" + pw);
  5. return "username=" + un + ", password=" + pw;
  6. }


方法四:
创建一个实体类对象作为参数承载体,spring会根据参数名称自动将参数绑定到实体类对象的属性上

  1. // 查询参数4
  2. @RequestMapping(value = "/test/query4", method = RequestMethod.GET)
  3. public String testQuery4(User user) {
  4. String username = user.getUsername();
  5. String password = user.getPassword();
  6. System.out.println("username=" + username + ", password=" + password);
  7. return "username=" + username + ", password=" + password;
  8. }

实体类定义如下:

  1. @Data
  2. @NoArgsConstructor
  3. @AllArgsConstructor
  4. @Builder
  5. public class User {
  6. private String username;
  7. private String password;
  8. }

这里用到了第三方库lombok,这样就不需要在代码中手动添加get、set等方法,lombok会自动添加。

发送请求的curl命令如下:

  1. curl -i 'http://192.168.1.14:8080/test/query1?username=aaa&password=bbb'

交互报文如下:

  1. GET /test/query1?username=aaa&password=bbb HTTP/1.1
  2. Host: 192.168.1.14:8080
  3. User-Agent: curl/7.58.0
  4. Accept: */*
  5.  
  6. HTTP/1.1 200
  7. Content-Type: text/plain;charset=UTF-8
  8. Content-Length: 26
  9. Date: Thu, 25 Oct 2018 07:01:30 GMT
  10. username=aaa, password=bbb

 

2 表单参数

请求参数不在url中,而是在Body体中,格式为:url?参数1=值1&参数2=值2...
适用于POST方式
表单参数处理方法和前面的请求参数处理方法几乎完全一样,只是RequestMethod注解中将method方法设置成POST方法

方法一:

  1. // 表单参数1
  2. @RequestMapping(value = "/test/form1", method = RequestMethod.POST)
  3. public String testForm1(String username, String password) {
  4. System.out.println("username=" + username + ", password=" + password);
  5. return "username=" + username + ", password=" + password;
  6. }

方法二:

  1. // 表单参数2
  2. @RequestMapping(value = "/test/form2", method = RequestMethod.POST)
  3. public String testForm2(HttpServletRequest request) {
  4. String username = request.getParameter("username");
  5. String password = request.getParameter("password");
  6. System.out.println("username=" + username + ", password=" + password);
  7. return "username=" + username + ", password=" + password;
  8. }

方法三:

  1. // 表单参数3
  2. @RequestMapping(value = "/test/form3", method = RequestMethod.POST)
  3. public String testForm3(@RequestParam("username") String un, @RequestParam("password") String pw) {
  4. System.out.println("username=" + un + ", password=" + pw);
  5. return "username=" + un + ", password=" + pw;
  6. }

方法四:

  1. // 表单参数4
  2. @RequestMapping(value = "/test/form4", method = RequestMethod.POST)
  3. public String testForm4(User user) {
  4. String username = user.getUsername();
  5. String password = user.getPassword();
  6. System.out.println("username=" + username + ", password=" + password);
  7. return "username=" + username + ", password=" + password;
  8. }

 curl请求命令如下:

  1. curl -X POST -i -d "username=aaa&password=bbb" http://192.168.1.14:8080/test/form1

请求和响应报文如下:

  1. POST /test/form1 HTTP/1.1
  2. Host: 192.168.1.14:8080
  3. User-Agent: curl/7.58.0
  4. Accept: */*
  5. Content-Length: 25
  6. Content-Type: application/x-www-form-urlencoded
  7. username=aaa&password=bbb
  8.  
  9. HTTP/1.1 200
  10. Content-Type: text/plain;charset=UTF-8
  11. Content-Length: 26
  12. Date: Thu, 25 Oct 2018 07:05:35 GMT
  13. username=aaa, password=bbb

 

3 路径参数

请求参数为url中的一部分,格式为:url/参数1/参数2...
同时适用于GET和POST方式
代码如下:

  1. @RequestMapping(value = "/test/url/{username}/{password}", method = RequestMethod.GET)
  2. public String testUrl(@PathVariable String username, @PathVariable String password) {
  3. System.out.println("username=" + username + ", password=" + password);
  4. return "username=" + username + ", password=" + password;
  5. }

请求curl命令如下:

  1. curl -i http://192.168.1.14:8080/test/url/aaa/bbb

请求和响应报文如下:

  1. GET /test/url/aaa/bbb HTTP/1.1
  2. Host: 192.168.1.14:8080
  3. User-Agent: curl/7.58.0
  4. Accept: */*
  5.  
  6. HTTP/1.1 200
  7. Content-Type: text/plain;charset=UTF-8
  8. Content-Length: 26
  9. Date: Thu, 25 Oct 2018 07:07:44 GMT
  10. username=aaa, password=bbb

 

4 json格式参数

请求参数在Body体中,并且为json格式。需要添加请求头:Content-Type: application/json;charset=UTF-8
适用于POST方式
方法一:
定义实体类,将json对象解析成实力类,需要添加RequestBody注解

  1. // json参数1
  2. @RequestMapping(value = "/test/json1", method = RequestMethod.POST)
  3. public String testJson1(@RequestBody User user) {
  4. String username = user.getUsername();
  5. String password = user.getPassword();
  6. System.out.println("username=" + username + ", password=" + password);
  7. return "username=" + username + ", password=" + password;
  8. }

方法二:
如果不像定义实体类,也可以将json请求直接解析成JSONObject类

  1. // json参数2
  2. @RequestMapping(value = "/test/json2", method = RequestMethod.POST)
  3. public String testJson2(@RequestBody JSONObject json) {
  4. String username = json.getString("username");
  5. String password = json.getString("password");
  6. System.out.println("username=" + username + ", password=" + password);
  7. return "username=" + username + ", password=" + password;
  8. }

方法三:
也可以将json对象直接解析成Map对象

  1. // json参数3
  2. @RequestMapping(value = "/test/json3", method = RequestMethod.POST)
  3. public String testJson3(@RequestBody Map<String, String> userMap) {
  4. String username = userMap.get("username");
  5. String password = userMap.get("password");
  6. System.out.println("username=" + username + ", password=" + password);
  7. return "username=" + username + ", password=" + password;
  8. }

请求curl命令如下:

  1. curl -X POST -i -H 'Content-Type: application/json;charset=UTF-8' -d '
  2. {
  3. "username" : "aaa",
  4. "password" : "bbb"
  5. }
  6. ' http://192.168.1.14:8080/test/json1

请求和响应报文如下:

  1. POST /test/json1 HTTP/1.1
  2. Host: 192.168.1.14:8080
  3. User-Agent: curl/7.58.0
  4. Accept: */*
  5. Content-Type: application/json;charset=UTF-8
  6. Content-Length: 52
  7. {
  8. "username" : "aaa",
  9. "password" : "bbb"
  10. }
  11. HTTP/1.1 200
  12. Content-Type: text/plain;charset=UTF-8
  13. Content-Length: 26
  14. Date: Thu, 25 Oct 2018 07:09:06 GMT
  15. username=aaa, password=bbb

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号