SpringMVC中Controller層獲取前端請求參數(shù)的方式匯總
1、SpringMVC自動綁定
在controller層方法中定義形參,若請求時參數(shù)名、參數(shù)類型相同,則SpringMVC會自動綁定。
/** * @description: 測試springmvc自動綁定 * @author: Vinci * @date: 2023/8/23 16:32 **/ @GetMapping("/automaticallyBindParameters") public String automaticallyBindParameters(String message,String test){ return "message="+message + ",test="+test; }
下面使用PostMan進行測試發(fā)現(xiàn),無論是將參數(shù)直接拼接到URL還是寫在form-data里;只要參數(shù)類型和參數(shù)名與方法形參相同,則會自動綁定
2、使用@RequestParam 注解進行接收
當controller層中的方法形參名與請求參數(shù)名不同時,可使用@RequestParam進行映射
@RequestParam中的value屬性指定請求參數(shù)名;
required屬性指定該參數(shù)是否是必須傳入的,默認是true(必須傳入) ;
defaultValue屬性來指定當前參數(shù)的默認值(當形參沒有傳入值時生效)
/** * @description: 測試@RequestParam注解 * @author: Vinci * @date: 2023/8/24 8:28 **/ @GetMapping("/testRequestParam") public String testRequestParam( @RequestParam(value = "number",required = false,defaultValue = "1") int i ){ return "number=" + i; }
接下來使用PostMan進行測試發(fā)現(xiàn),傳值則接收到值,不傳值則使用默認值
3、@RequestBody注解
@RequestBody 注解 可將 JSON數(shù)據(jù) 轉(zhuǎn)為 Bean對象, 與@ResponseBody注解作用相反
(1) 使用實體來接收JSON
創(chuàng)建實體對象
@Data @NoArgsConstructor @AllArgsConstructor public class User { private String id; private String username; private String password; }
使用RequestBody接收
/** * @description: 使用實體接收JSON * @author: Vinci * @date: 2023/8/24 9:01 **/ @GetMapping("/testReceiveJson") public String testReceiveJson(@RequestBody User user){ return user.toString(); }
PostMan測試結(jié)果
(2)使用 Map 集合接收JSON
/** * @description: 使用Map接收JSON * @author: Vinci * @date: 2023/8/24 9:15 **/ @GetMapping("/testMapReceiveJson") public String testMapReceiveJson(@RequestBody Map<String,Object> maps){ return maps.toString(); }
PostMan測試結(jié)果
(3) 使用 List集合接收JSON數(shù)組
/** * @description: 使用List集合接收JSON數(shù)組 * @author: Vinci * @date: 2023/8/24 9:17 **/ @GetMapping("/testListJson") public String testListJson(@RequestBody List<User> users){ return users.toString(); }
PostMan測試結(jié)果
4、@PathVariable 注解
可以使用@PathVariable注解獲取url地址欄指定位置參數(shù),url中的參數(shù)名需和controller形參一致,或是使用@PathVariable注解的value屬性進行映射,在寫@XXXMapping注解中的url時參數(shù)名需要使用 { } 括起來
/** * @description: 使用@PathVariable注解獲取地址欄指定位置參數(shù) * @author: Vinci * @date: 2023/8/24 9:32 **/ @GetMapping("/testGetAddressBarParameters/{id}") public String addressBarParameters(@PathVariable int id){ return String.valueOf(id); }
PostMan測試結(jié)果
到此這篇關(guān)于SpringMVC中Controller層獲取前端請求參數(shù)的幾種方式的文章就介紹到這了,更多相關(guān)SpringMVC Controller層獲取前端請求參數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring cloud gateway設(shè)置context-path服務(wù)路由404排查過程
這篇文章主要介紹了Spring cloud gateway設(shè)置context-path服務(wù)路由404排查過程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08Spring SpringMVC在啟動完成后執(zhí)行方法源碼解析
這篇文章主要介紹了SpringMVC在啟動完成后執(zhí)行方法源碼解析,還是非常不錯的,在這里分享給大家,需要的朋友可以參考下。2017-09-09Java @Value("${xxx}")取properties時中文亂碼的解決
這篇文章主要介紹了Java @Value("${xxx}")取properties時中文亂碼的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07Java把Map轉(zhuǎn)為對象的實現(xiàn)代碼
在項目開發(fā)中,經(jīng)常碰到map轉(zhuǎn)實體對象或者對象轉(zhuǎn)map的場景,工作中,很多時候我們可能比較喜歡使用第三方j(luò)ar包的API對他們進行轉(zhuǎn)化,但這里,我想通過反射的方式對他們做轉(zhuǎn)化,感興趣的同學(xué)跟著小編來看看吧2023-08-08使用Java8?Stream流的skip?+?limit實現(xiàn)批處理的方法
Stream 作為 Java 8 的一大亮點,它與 java.io 包里的 InputStream 和 OutputStream 是完全不同的概念這篇文章主要介紹了使用Java8?Stream流的skip?+?limit實現(xiàn)批處理,需要的朋友可以參考下2022-07-07