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請求參數內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- SpringMVC中請求參數的獲取方式
- SpringBoot獲取前臺參數的六種方式以及統一響應
- SpringMVC如何接收參數各種場景
- java fastjson傳輸long數據卻接收到瞭int的問題
- SpringMVC如何獲取表單數據(radio和checkbox)