SpringMvc接受请求参数的几种情况演示
说明:
通常get请求获取的参数是在url后面,而post请求获取的是请求体当中的参数。因此两者在请求方式上会有所不同。
1.直接将接受的参数写在controller对应方法的形参当中(适用于get提交方式)
/** * 1.直接把表单的参数写在Controller相应的方法的形参中 * * @param username * @param password * @return */ @GetMapping("/addUser1") public String addUser1(String username, String password) { System.out.println ("username is:" + username); System.out.println ("password is:" + password); return username + "," + password; }
2.通过url请求路径获取参数
/** * 2、通过@PathVariable获取路径中的参数 * * @param username * @param password * @return */ @RequestMapping(value = "/addUser4/{username}/{password}", method = RequestMethod.GET) public String addUser4(@PathVariable String username, @PathVariable String password) { System.out.println ("username is:" + username); System.out.println ("password is:" + password); return "addUser4"; }
3.通过request请求对象来接受发来的参数信息(Get请求方式或者时Post请求方式都可以)
/** * 3、通过HttpServletRequest接收 * * @param request * @return */ @RequestMapping("/addUser2") public String addUser2(HttpServletRequest request) { String username = request.getParameter ("username"); String password = request.getParameter ("password"); System.out.println ("username is:" + username); System.out.println ("password is:" + password); return "demo/index"; }
4.封装JavaBean对象的方式来接受请求参数(get方式与post方式都可以)
4.1首先在模块当中创建对应的JavaBean,并提供相应的get,set方法。
package com.example.demo.pojo; import lombok.Data; @Data public class User1 { private String username; private String password; }
4.2Controller层
/** * 4、通过一个bean来接收 * * @param user * @return */ @RequestMapping("/addUser3") public String addUser3(User1 user) { System.out.println ("username is:" + user.getUsername ( )); System.out.println ("password is:" + user.getPassword ( )); return "/addUser3"; }
5.使用注解@RequestParam注解将请求参数绑定到Controller层对应方法的形参当中
/** * 5、用注解@RequestParam绑定请求参数到方法入参 * @param username * @param password * @return */ @RequestMapping(value="/addUser6",method=RequestMethod.GET) public String addUser6(@RequestParam("username") String username,@RequestParam("password") String password) { System.out.println("username is:"+username); System.out.println("password is:"+password); return "demo/index"; }
下面介绍,发送json格式的请求,接受数据的情况:
1.将json请求的key,value值封装到实体对象的属性当中(通常将参数放在请求体body中,以application/json格式被后端获取)
1.1创建一个实体类
public class User2 implements Serializable { private static final long serialVersionUID = 1L; @JsonProperty(value = "id") private Integer id; @JsonProperty(value = "name") private String name; @JsonProperty(value = "age") private Integer age; @JsonProperty(value = "hobby") private List<String> hobby;
/** *将json请求的key,value封装到实体对象当中。 * @param user * @return */ @PostMapping("/save") public String saveUser(@RequestBody User2 user) { // list.add(user); // User2 user2 = new User2 ( ); // user2.setId (user.getId ()); // user2.setAge (user.getAge ()); // user2.setName (user.getName ()); // user2.setHobby (user.getHobby ()); return "success"+user; }
2.将json请求的key,value值封装到request对象的属性当中(通常请求参数放body中,将content-type改为x-www-form-urlencoded)
/** * 将请求参数封装到request对象当中。 * @param request * @return */ @PostMapping("/save2") public User2 save(HttpServletRequest request) { Integer id = Integer.parseInt(request.getParameter("id")); String name = request.getParameter("name"); Integer age = Integer.parseInt(request.getParameter("age")); String parameter = request.getParameter("hobby"); List<String> stringList = new ArrayList<> ( ); String[] split = parameter.split (","); for (int i = split.length - 1; i >= 0; i--) { stringList.add (split[i]); } User2 user2 = new User2(id, name, age, stringList); // list.add(user); return user2; }
3.通过http协议,将json参数转成JSONOBject对象
3.1Controller层接受JSON参数
/** * 通过http协议将参数转为jsonobject * @param request * @return * @throws IOException * @throws JSONException */ @PostMapping("/save3") public User2 save3(HttpServletRequest request) throws IOException, JSONException { JSONObject jsonObject = handlerData(request); Integer id = jsonObject.getInteger("id"); String name = jsonObject.getString("name"); Integer age = jsonObject.getInteger("age"); List<String> hobby = jsonObject.getObject("hobby", List.class); User2 user3 = new User2 (id, name, age, hobby); // list.add(user); return user3; }
3.2通过以下方法将Json字符串转成Jsonobject对象
//这里使用的是alibaba的json工具类 public static JSONObject handlerData(HttpServletRequest request) throws IOException, JSONException { StringBuffer sb = new StringBuffer(); InputStream is = request.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader (is, "utf-8")); String s = ""; while ((s = br.readLine()) != null) { sb.append(s); } if (sb.toString().length() <= 0) { return null; } else { return JSONObject.parseObject(sb.toString()); } }
4.将json格式的请求参数封装到hashmap的key,value键-值对当中。(json字符串串放在body中,请求格式为application/json格式)
/** *将json请求的Key,value封装到map的key,value当中去。 * @param map * @return */ @PostMapping("/save1") public User2 saveUser1(@RequestBody Map<String, Object> map) { Integer id = (Integer) map.get("id"); String name = (String) map.get("name"); Integer age = (Integer) map.get("age"); List<String> hobby=(List<String>) map.get("hobby"); User2 user = new User2(id, name, age, hobby); // list.add(user); return user; }
该文档主要是学习以下两篇文档的总结:
https://www.cnblogs.com/lirenhe/p/10737673.html
https://blog.csdn.net/zyxwvuuvwxyz/article/details/80352712
到此这篇关于SpringMvc接受请求参数的几种情况演示的文章就介绍到这了,更多相关SpringMvc请求参数内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!