//原始方式
@RequestMapping("/simpleParam")
public String simpleParam(HttpServletRequest request){
//獲取請求引數
String name = request.getParameter("name");
String age = request.getParameter("age");
int age1 = Integer.parseInt(age);
System.out.println(name+":"+ age1);
return "ok";
}
//springboot方式
@RequestMapping("/simpleParam")
public String simpleParam(String name,Integer age){
//獲取請求引數
System.out.println(name+":"+ age);
return "ok";
}
**
**
@RequestParam註解的使用:方法形參名稱與請求引數名稱不匹配,可以使用@RequestParam完成對映。
@RequestMapping("/simpleParam")
public String simpleParam(@RequestParam(name="name",required = false) String username,Integer age){
//獲取請求引數
System.out.println(username+":"+ age);
return "ok";
}
@RequestParam中的required屬性預設為true,代表該請求引數必須傳遞,如果不傳遞將報錯,如果該引數是可選中,可以將required屬性設定為false。
如下圖所示:
規則:請求引數名與形參物件屬性名相同,即可直接通過pojo接收。
User實體類
public class User {
private String name;
private Integer age;
private Address address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", address=" + address +
'}';
}
Address實體類
public class Address {
private String province;
private String city;
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return "Address{" +
"province='" + province + '\'' +
", city='" + city + '\'' +
'}';
}
陣列引數: 請求引數名與形引陣列名稱相同且請求引數為多個,定義陣列類形參即可接受引數。
//陣列集合引數
@RequestMapping("/arrayParam")
public String arrayParam(String[] hobby){
System.out.println(Arrays.toString(hobby));
return "ok";
}
集合引數: 請求引數名與形參集合名稱相同且請求引數為多個,@RequestParam繫結引數關係
@RequestMapping("/listParam")
public String listParam(@RequestParam List<String> hobby){
System.out.println(hobby);
return "ok";
}
小結:
陣列:請求引數名與形參中陣列變數名相同,可以直接使用陣列封裝
集合:請求引數名與形參中集合變數名相同,通過@RequestParam繫結引數關係
日期引數: 使用@DateTimeFormat註解完成日期引數格式轉換
//日期時間引數
@RequestMapping("/dateParam")
public String dateParam(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime updateTime){
System.out.println(updateTime);
return "ok";
}
Json引數: JSON資料鍵名與形參物件屬性名相同,定義POJO型別形參即可接收引數,需要使用@RequestBody標識
//json引數
@RequestMapping("/jsonParam")
public String jsonParam(@RequestBody User user){
System.out.println(user);
return "ok";
}
路徑引數: 通過請求url直接傳遞引數,使用{...}來標識該路徑引數,需要使用@PathVariable獲取路徑引數
//路徑引數
@RequestMapping("/path/{id}")
public String pathParam(@PathVariable Integer id){
System.out.println(id);
return "ok";
}
@RequestMapping("/path/{id}/{name}")
public String pathParam2(@PathVariable Integer id,@PathVariable String name){
System.out.println(id+":"+name);
return "ok";
}
簡單引數:
實體引數:
陣列集合引數:
日期引數:
JSON引數:
路徑引數: