springboot3請求參數(shù)種類及接口測試案例小結(jié)
SpringBoot3數(shù)據(jù)請求:
原始數(shù)據(jù)請求:
//原始方式 @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ù)據(jù)請求方式:
//springboot方式 @RequestMapping("/simpleParam") public String simpleParam(String name,Integer age){ //獲取請求參數(shù) System.out.println(name+":"+ age); return "ok"; }
**
一、簡單實(shí)體參數(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屬性默認(rèn)為true,代表該請求參數(shù)必須傳遞,如果不傳遞將報(bào)錯(cuò),如果該參數(shù)是可選中,可以將required屬性設(shè)置為false。
如下圖所示:
二、實(shí)體對象參數(shù)
規(guī)則:請求參數(shù)名與形參對象屬性名相同,即可直接通過pojo接收。
User實(shí)體類
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實(shí)體類
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ù)為多個(gè),定義數(shù)組類形參即可接受參數(shù)。
//數(shù)組集合參數(shù) @RequestMapping("/arrayParam") public String arrayParam(String[] hobby){ System.out.println(Arrays.toString(hobby)); return "ok"; }
集合參數(shù): 請求參數(shù)名與形參集合名稱相同且請求參數(shù)為多個(gè),@RequestParam綁定參數(shù)關(guān)系
@RequestMapping("/listParam") public String listParam(@RequestParam List<String> hobby){ System.out.println(hobby); return "ok"; }
小結(jié):
數(shù)組:請求參數(shù)名與形參中數(shù)組變量名相同,可以直接使用數(shù)組封裝
集合:請求參數(shù)名與形參中集合變量名相同,通過@RequestParam綁定參數(shù)關(guān)系
四、日期參數(shù)
日期參數(shù): 使用@DateTimeFormat注解完成日期參數(shù)格式轉(zhuǎn)換
//日期時(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ù)據(jù)鍵名與形參對象屬性名相同,定義POJO類型形參即可接收參數(shù),需要使用@RequestBody標(biāo)識
//json參數(shù) @RequestMapping("/jsonParam") public String jsonParam(@RequestBody User user){ System.out.println(user); return "ok"; }
六、路徑參數(shù)
路徑參數(shù): 通過請求url直接傳遞參數(shù),使用{...}來標(biāo)識該路徑參數(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"; }
總結(jié)
簡單參數(shù):
- 定義方法形參,請求參數(shù)名與形參變量名一致
- 如果不一致,通過@RequestParam手動映射
實(shí)體參數(shù):
- 請求參數(shù)名,與實(shí)體對象的屬性名一致,會自動接受封裝
數(shù)組集合參數(shù):
- 數(shù)組:請求參數(shù)名與數(shù)組名一致,直接封裝
- 集合:請求參數(shù)名與集合名一致,@RequestParam綁定關(guān)系
日期參數(shù):
- @DateTimeFormat
JSON參數(shù):
- @RequestBody
路徑參數(shù):
- PathVariable
到此這篇關(guān)于springboot3請求參數(shù)種類及接口測試案例小結(jié)的文章就介紹到這了,更多相關(guān)springboot請求參數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot如何獲取Get請求參數(shù)詳解
- springBoot 過濾器去除請求參數(shù)前后空格實(shí)例詳解
- SpringBoot通過JSON傳遞請求參數(shù)的實(shí)例詳解
- springboot如何設(shè)置請求參數(shù)長度和文件大小限制
- SpringBoot常見get/post請求參數(shù)處理、參數(shù)注解校驗(yàn)及參數(shù)自定義注解校驗(yàn)詳解
- SpringBoot之自定義Filter獲取請求參數(shù)與響應(yīng)結(jié)果案例詳解
- springboot?vue接口測試HutoolUtil?TreeUtil處理樹形結(jié)構(gòu)
- springboot?vue接口測試前后端實(shí)現(xiàn)模塊樹列表功能
- springboot?vue接口測試前后端樹節(jié)點(diǎn)編輯刪除功能
- springboot?vue接口測試前端動態(tài)增刪表單功能實(shí)現(xiàn)
相關(guān)文章
SpringBoot實(shí)現(xiàn)微服務(wù)通信的多種方式
微服務(wù)通信是指在分布式系統(tǒng)中,各個(gè)微服務(wù)之間進(jìn)行數(shù)據(jù)交互和通信的過程,今天我們將探討在Spring Boot中實(shí)現(xiàn)微服務(wù)通信的多種方式,文章通過代碼示例給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-07-07SpringCloud Config分布式配置中心使用教程介紹
springcloud config是一個(gè)解決分布式系統(tǒng)的配置管理方案。它包含了 client和server兩個(gè)部分,server端提供配置文件的存儲、以接口的形式將配置文件的內(nèi)容提供出去,client端通過接口獲取數(shù)據(jù)、并依據(jù)此數(shù)據(jù)初始化自己的應(yīng)用2022-12-12Java+MySql圖片數(shù)據(jù)保存與讀取的具體實(shí)例
之前一直沒有做過涉及到圖片存儲的應(yīng)用,最近要做的東東涉及到了這個(gè)點(diǎn),就做了一個(gè)小的例子算是對圖片存儲的初試吧2013-06-06Java探索之Thread+IO文件的加密解密代碼實(shí)例
這篇文章主要介紹了Java探索之Thread+IO文件的加密解密代碼實(shí)例,具有一定參考價(jià)值,需要的朋友可以了解下。2017-10-10SpringBoot使用@ControllerAdvice全局異常處理
這篇文章主要介紹了SpringBoot使用@ControllerAdvice全局異常處理,異常處理是非常重要的一部分,它可以幫助我們捕獲并處理應(yīng)用程序中出現(xiàn)的異常情況,提高應(yīng)用程序的健壯性和可靠性,需要的朋友可以參考下2023-07-07Spring?AOP利用切面實(shí)現(xiàn)日志保存的示例詳解
最近領(lǐng)導(dǎo)讓寫個(gè)用切面實(shí)現(xiàn)日志保存,經(jīng)過調(diào)研和親測,以完美解決。在這里分享給大家,給有需要的碼友直接使用,希望對大家有所幫助2022-11-11