springboot3請求參數(shù)種類及接口測試案例小結
SpringBoot3數(shù)據請求:
原始數(shù)據請求:
//原始方式
@RequestMapping("/simpleParam")
public String simpleParam(HttpServletRequest request){
//獲取請求參數(shù)
String name = request.getParameter("name");
String age = request.getParameter("age");
int age1 = Integer.parseInt(age);
System.out.println(name+":"+ age1);
return "ok";
}springboot數(shù)據請求方式:
//springboot方式
@RequestMapping("/simpleParam")
public String simpleParam(String name,Integer age){
//獲取請求參數(shù)
System.out.println(name+":"+ age);
return "ok";
}**
一、簡單實體參數(shù):
**
@RequestParam注解的使用:方法形參名稱與請求參數(shù)名稱不匹配,可以使用@RequestParam完成映射。


@RequestMapping("/simpleParam")
public String simpleParam(@RequestParam(name="name",required = false) String username,Integer age){
//獲取請求參數(shù)
System.out.println(username+":"+ age);
return "ok";
}@RequestParam中的required屬性默認為true,代表該請求參數(shù)必須傳遞,如果不傳遞將報錯,如果該參數(shù)是可選中,可以將required屬性設置為false。
如下圖所示:


二、實體對象參數(shù)
規(guī)則:請求參數(shù)名與形參對象屬性名相同,即可直接通過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 + '\'' +
'}';
}

三、數(shù)組集合參數(shù):
數(shù)組參數(shù): 請求參數(shù)名與形參數(shù)組名稱相同且請求參數(shù)為多個,定義數(shù)組類形參即可接受參數(shù)。
//數(shù)組集合參數(shù)
@RequestMapping("/arrayParam")
public String arrayParam(String[] hobby){
System.out.println(Arrays.toString(hobby));
return "ok";
}


集合參數(shù): 請求參數(shù)名與形參集合名稱相同且請求參數(shù)為多個,@RequestParam綁定參數(shù)關系
@RequestMapping("/listParam")
public String listParam(@RequestParam List<String> hobby){
System.out.println(hobby);
return "ok";
}

小結:
數(shù)組:請求參數(shù)名與形參中數(shù)組變量名相同,可以直接使用數(shù)組封裝
集合:請求參數(shù)名與形參中集合變量名相同,通過@RequestParam綁定參數(shù)關系
四、日期參數(shù)
日期參數(shù): 使用@DateTimeFormat注解完成日期參數(shù)格式轉換
//日期時間參數(shù)
@RequestMapping("/dateParam")
public String dateParam(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime updateTime){
System.out.println(updateTime);
return "ok";
}


五、Json參數(shù)
Json參數(shù): JSON數(shù)據鍵名與形參對象屬性名相同,定義POJO類型形參即可接收參數(shù),需要使用@RequestBody標識
//json參數(shù)
@RequestMapping("/jsonParam")
public String jsonParam(@RequestBody User user){
System.out.println(user);
return "ok";
}


六、路徑參數(shù)
路徑參數(shù): 通過請求url直接傳遞參數(shù),使用{...}來標識該路徑參數(shù),需要使用@PathVariable獲取路徑參數(shù)
//路徑參數(shù)
@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";
}


總結
簡單參數(shù):
- 定義方法形參,請求參數(shù)名與形參變量名一致
- 如果不一致,通過@RequestParam手動映射
實體參數(shù):
- 請求參數(shù)名,與實體對象的屬性名一致,會自動接受封裝
數(shù)組集合參數(shù):
- 數(shù)組:請求參數(shù)名與數(shù)組名一致,直接封裝
- 集合:請求參數(shù)名與集合名一致,@RequestParam綁定關系
日期參數(shù):
- @DateTimeFormat
JSON參數(shù):
- @RequestBody
路徑參數(shù):
- PathVariable
到此這篇關于springboot3請求參數(shù)種類及接口測試案例小結的文章就介紹到這了,更多相關springboot請求參數(shù)內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- SpringBoot如何獲取Get請求參數(shù)詳解
- springBoot 過濾器去除請求參數(shù)前后空格實例詳解
- SpringBoot通過JSON傳遞請求參數(shù)的實例詳解
- springboot如何設置請求參數(shù)長度和文件大小限制
- SpringBoot常見get/post請求參數(shù)處理、參數(shù)注解校驗及參數(shù)自定義注解校驗詳解
- SpringBoot之自定義Filter獲取請求參數(shù)與響應結果案例詳解
- springboot?vue接口測試HutoolUtil?TreeUtil處理樹形結構
- springboot?vue接口測試前后端實現(xiàn)模塊樹列表功能
- springboot?vue接口測試前后端樹節(jié)點編輯刪除功能
- springboot?vue接口測試前端動態(tài)增刪表單功能實現(xiàn)
相關文章
java反射機制及beanUtils的實現(xiàn)原理分析
本文介紹了Java的反射機制、VO、DTO、PO的概念以及BeanUtils的實現(xiàn)原理和簡單示例,通過反射可以在運行時動態(tài)操作類、方法和字段,BeanUtils用于在不同bean之間進行屬性復制2024-12-12
BeanUtils.copyProperties使用總結以及注意事項說明
這篇文章主要介紹了BeanUtils.copyProperties使用總結以及注意事項說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
IntelliJ IDEA 創(chuàng)建 Java 項目及創(chuàng)建 Java 文件并運行的詳細步驟
這篇文章主要介紹了IntelliJ IDEA 創(chuàng)建 Java 項目及創(chuàng)建 Java 文件并運行的詳細步驟,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11
通過Java?Reflection實現(xiàn)編譯時注解正確處理方法
Java注解是一種標記在JDK5及以后的版本中引入,用于Java語言中向程序添加元數(shù)據的方法,這篇文章主要介紹了通過Java?Reflection實現(xiàn)編譯時注解處理方法,需要的朋友可以參考下2023-06-06
簡單了解SpringMVC緩存對靜態(tài)資源有什么影響
這篇文章主要介紹了簡單了解SpringMVC緩存對靜態(tài)資源有什么影響,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-09-09
JDK21中Sequenced Collections(序列集合)的實現(xiàn)
Sequenced Collections是Java中的一個新特性,它提供了一種有序的集合實現(xiàn),本文主要介紹了JDK21中Sequenced Collections(序列集合)的實現(xiàn),感興趣的可以了解一下2025-09-09
解決Java Redis刪除HashMap中的key踩到的坑
這篇文章主要介紹了解決Java Redis刪除HashMap中的key踩到的坑,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02

