SpringMvc接受请求参数的几种情况演示

时间:2021-02-06 07:11:30

说明

  通常get请求获取的参数是在url后面,而post请求获取的是请求体当中的参数。因此两者在请求方式上会有所不同。  

1.直接将接受的参数写在controller对应方法的形参当中(适用于get提交方式

 1 /**
2 * 1.直接把表单的参数写在Controller相应的方法的形参中
3 *
4 * @param username
5 * @param password
6 * @return
7 */
8 @GetMapping("/addUser1")
9 public String addUser1(String username, String password) {
10 System.out.println ("username is:" + username);
11 System.out.println ("password is:" + password);
12 return username + "," + password;
13 }

  2.通过url请求路径获取参数

 1  /**
2 * 2、通过@PathVariable获取路径中的参数
3 *
4 * @param username
5 * @param password
6 * @return
7 */
8 @RequestMapping(value = "/addUser4/{username}/{password}", method = RequestMethod.GET)
9 public String addUser4(@PathVariable String username, @PathVariable String password) {
10 System.out.println ("username is:" + username);
11 System.out.println ("password is:" + password);
12 return "addUser4";
13 }

  3.通过request请求对象来接受发来的参数信息(Get请求方式或者时Post请求方式都可以

 1     /**
2 * 3、通过HttpServletRequest接收
3 *
4 * @param request
5 * @return
6 */
7 @RequestMapping("/addUser2")
8 public String addUser2(HttpServletRequest request) {
9 String username = request.getParameter ("username");
10 String password = request.getParameter ("password");
11 System.out.println ("username is:" + username);
12 System.out.println ("password is:" + password);
13 return "demo/index";
14 }

  4.封装JavaBean对象的方式来接受请求参数(get方式与post方式都可以

    4.1首先在模块当中创建对应的JavaBean,并提供相应的get,set方法。

1 package com.example.demo.pojo;
2
3 import lombok.Data;
4
5 @Data
6 public class User1 {
7 private String username;
8 private String password;
9 }

    4.2Controller层

 1     /**
2 * 4、通过一个bean来接收
3 *
4 * @param user
5 * @return
6 */
7 @RequestMapping("/addUser3")
8 public String addUser3(User1 user) {
9 System.out.println ("username is:" + user.getUsername ( ));
10 System.out.println ("password is:" + user.getPassword ( ));
11 return "/addUser3";
12 }

  5.使用注解@RequestParam注解将请求参数绑定到Controller层对应方法的形参当中

 1     /**
2 * 5、用注解@RequestParam绑定请求参数到方法入参
3 * @param username
4 * @param password
5 * @return
6 */
7 @RequestMapping(value="/addUser6",method=RequestMethod.GET)
8 public String addUser6(@RequestParam("username") String username,@RequestParam("password") String password) {
9 System.out.println("username is:"+username);
10 System.out.println("password is:"+password);
11 return "demo/index";
12 }

下面介绍,发送json格式的请求,接受数据的情况:

  1.将json请求的key,value值封装到实体对象的属性当中(通常将参数放在请求体body中,以application/json格式被后端获取

    1.1创建一个实体类

 1 public class User2 implements Serializable {
2 private static final long serialVersionUID = 1L;
3 @JsonProperty(value = "id")
4 private Integer id;
5 @JsonProperty(value = "name")
6 private String name;
7 @JsonProperty(value = "age")
8 private Integer age;
9 @JsonProperty(value = "hobby")
10 private List<String> hobby;
 1     /**
2 *将json请求的key,value封装到实体对象当中。
3 * @param user
4 * @return
5 */
6 @PostMapping("/save")
7 public String saveUser(@RequestBody User2 user) {
8 // list.add(user);
9 // User2 user2 = new User2 ( );
10 // user2.setId (user.getId ());
11 // user2.setAge (user.getAge ());
12 // user2.setName (user.getName ());
13 // user2.setHobby (user.getHobby ());
14 return "success"+user;
15 }

2.将json请求的key,value值封装到request对象的属性当中(通常请求参数放body中,将content-type改为x-www-form-urlencoded)

 1 /**
2 * 将请求参数封装到request对象当中。
3 * @param request
4 * @return
5 */
6 @PostMapping("/save2")
7 public User2 save(HttpServletRequest request) {
8 Integer id = Integer.parseInt(request.getParameter("id"));
9 String name = request.getParameter("name");
10 Integer age = Integer.parseInt(request.getParameter("age"));
11 String parameter = request.getParameter("hobby");
12 List<String> stringList = new ArrayList<> ( );
13
14 String[] split = parameter.split (",");
15 for (int i = split.length - 1; i >= 0; i--) {
16 stringList.add (split[i]);
17 }
18
19 User2 user2 = new User2(id, name, age, stringList);
20 // list.add(user);
21 return user2;
22 }

3.通过http协议,将json参数转成JSONOBject对象

  3.1Controller层接受JSON参数

 1 /**
2 * 通过http协议将参数转为jsonobject
3 * @param request
4 * @return
5 * @throws IOException
6 * @throws JSONException
7 */
8 @PostMapping("/save3")
9 public User2 save3(HttpServletRequest request) throws IOException, JSONException {
10
11 JSONObject jsonObject = handlerData(request);
12 Integer id = jsonObject.getInteger("id");
13 String name = jsonObject.getString("name");
14 Integer age = jsonObject.getInteger("age");
15 List<String> hobby = jsonObject.getObject("hobby", List.class);
16 User2 user3 = new User2 (id, name, age, hobby);
17 // list.add(user);
18 return user3;
19 }

  3.2通过以下方法将Json字符串转成Jsonobject对象

 1 //这里使用的是alibaba的json工具类
2 public static JSONObject handlerData(HttpServletRequest request) throws IOException, JSONException {
3 StringBuffer sb = new StringBuffer();
4 InputStream is = request.getInputStream();
5 BufferedReader br = new BufferedReader(new InputStreamReader (is, "utf-8"));
6 String s = "";
7 while ((s = br.readLine()) != null) {
8 sb.append(s);
9 }
10 if (sb.toString().length() <= 0) {
11 return null;
12 } else {
13 return JSONObject.parseObject(sb.toString());
14 }
15 }

  4.将json格式的请求参数封装到hashmap的key,value键-值对当中。(json字符串串放在body中,请求格式为application/json格式)

 1     /**
2 *将json请求的Key,value封装到map的key,value当中去。
3 * @param map
4 * @return
5 */
6 @PostMapping("/save1")
7 public User2 saveUser1(@RequestBody Map<String, Object> map) {
8 Integer id = (Integer) map.get("id");
9 String name = (String) map.get("name");
10 Integer age = (Integer) map.get("age");
11 List<String> hobby=(List<String>) map.get("hobby");
12 User2 user = new User2(id, name, age, hobby);
13 // list.add(user);
14 return user;
15 }

该文档主要是学习以下两篇文档的总结:

https://www.cnblogs.com/lirenhe/p/10737673.html

https://blog.csdn.net/zyxwvuuvwxyz/article/details/80352712