Springboot中@RequestParam和@PathVariable的用法與區(qū)別詳解
@RequestParam和@PathVariable的用法
RESTful API設計的最佳實踐是使用路徑參數(shù)來標識一個或多個特定資源,而使用查詢參數(shù)來對這些資源進行排序/過濾
@PathVariable
會用在單個對象的查詢上,比如要根據ID值查詢學生信息,就會在Postman發(fā)送GET請求,后臺使用@PathVariable接收
后端是
@RequestMapping(value="/page/{name}/{age}",method=RequestMethod.GET)
public String getName(ModelMap map,@PathVariable("name") String name,@PathVariable("age") int age)
{
map.addAttribute("name",name);
map.addAttribute("age",age);
return "name";
}
接口樣式是
//localhost:8080/page/xiaoming/18
@RequestParam
會用在組合查詢多個對象,比如跟據姓名模糊查詢和性別組合查詢篩選學生,就會發(fā)送POST請求,后臺使用RequestParam接收 后端:
@RequestMapping(value="/result",method=RequestMethod.GET)
public String resultParam(ModelMap map,@RequestParam String name,@RequestParam int age)
{
map.addAttribute("name",name);
map.addAttribute("age",age);
return "result";
}
接口樣式:
//localhost:8080/result?name=xiaoming&age=20
區(qū)別
1、當URL指向的是某一具體業(yè)務資源(或資源列表),例如博客,用戶時,使用@PathVariable
這個是舉例是為了獲取具體某一個缺陷或者用戶的時候用
2、當URL需要對資源或者資源列表進行過濾,篩選時,用@RequestParam
到此這篇關于Springboot中@RequestParam和@PathVariable的用法與區(qū)別詳解的文章就介紹到這了,更多相關@RequestParam和@PathVariable的用法內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- springboot中@RequestParam和@PathVariable區(qū)別
- SpringBoot中@PathVariable、@RequestParam和@RequestBody的區(qū)別和使用詳解
- Spring中@PathVariable和@RequestParam注解的用法區(qū)別
- Spring中@RequestParam、@RequestBody和@PathVariable的用法詳解
- @PathVariable、@RequestParam和@RequestBody的區(qū)別
- 方法參數(shù)屬性params,@PathVariable和@RequestParam用法及區(qū)別
- @PathVariable和@RequestParam傳參為空問題及解決
- 使用@pathvariable與@requestparam碰到的一些問題及解決
- 聊聊@RequestParam,@PathParam,@PathVariable等注解的區(qū)別
- Java中@PathVariable 和 @RequestParam的區(qū)別小結
相關文章
SpringMVC數(shù)據頁響應ModelAndView實現(xiàn)頁面跳轉
本文主要介紹了SpringMVC數(shù)據頁響應ModelAndView實現(xiàn)頁面跳轉,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-07-07
java使用POI讀取properties文件并寫到Excel的方法
這篇文章主要介紹了java使用POI讀取properties文件并寫到Excel的方法,涉及java操作properties文件及Excel文件的相關技巧,需要的朋友可以參考下2015-06-06
SpringBoot自動裝配Condition的實現(xiàn)方式
這篇文章主要介紹了SpringBoot自動裝配Condition的實現(xiàn)方式,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08

