說明:
通常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