SpringMvc接受請求參數(shù)的幾種情況演示
說明:
通常get請求獲取的參數(shù)是在url后面,而post請求獲取的是請求體當中的參數(shù)。因此兩者在請求方式上會有所不同。
1.直接將接受的參數(shù)寫在controller對應方法的形參當中(適用于get提交方式)
/**
* 1.直接把表單的參數(shù)寫在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請求路徑獲取參數(shù)
/**
* 2、通過@PathVariable獲取路徑中的參數(shù)
*
* @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請求對象來接受發(fā)來的參數(shù)信息(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對象的方式來接受請求參數(shù)(get方式與post方式都可以)
4.1首先在模塊當中創(chuàng)建對應的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注解將請求參數(shù)綁定到Controller層對應方法的形參當中
/**
* 5、用注解@RequestParam綁定請求參數(shù)到方法入?yún)?
* @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";
}
下面介紹,發(fā)送json格式的請求,接受數(shù)據(jù)的情況:
1.將json請求的key,value值封裝到實體對象的屬性當中(通常將參數(shù)放在請求體body中,以application/json格式被后端獲?。?/p>
1.1創(chuàng)建一個實體類
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對象的屬性當中(通常請求參數(shù)放body中,將content-type改為x-www-form-urlencoded)
/**
* 將請求參數(shù)封裝到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協(xié)議,將json參數(shù)轉成JSONOBject對象
3.1Controller層接受JSON參數(shù)
/**
* 通過http協(xié)議將參數(shù)轉為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格式的請求參數(shù)封裝到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接受請求參數(shù)的幾種情況演示的文章就介紹到這了,更多相關SpringMvc請求參數(shù)內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
idea創(chuàng)建包含多個springboot module的maven project的方法
這篇文章主要介紹了idea創(chuàng)建包含多個springboot module的maven project的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
Springboot結合JDBC實現(xiàn)雙數(shù)據(jù)源實例
這篇文章主要為大家介紹了Springboot結合JDBC實現(xiàn)雙數(shù)據(jù)源實例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12
springboot?ErrorPageFilter的實際應用詳解
這篇文章主要介紹了springboot?ErrorPageFilter的實際應用詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01

