Springboot中@RequestParam和@PathVariable的用法與區(qū)別詳解
@RequestParam和@PathVariable的用法
RESTful API設(shè)計(jì)的最佳實(shí)踐是使用路徑參數(shù)來標(biāo)識一個(gè)或多個(gè)特定資源,而使用查詢參數(shù)來對這些資源進(jìn)行排序/過濾
@PathVariable
會(huì)用在單個(gè)對象的查詢上,比如要根據(jù)ID值查詢學(xué)生信息,就會(huì)在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
會(huì)用在組合查詢多個(gè)對象,比如跟據(jù)姓名模糊查詢和性別組合查詢篩選學(xué)生,就會(huì)發(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、當(dāng)URL指向的是某一具體業(yè)務(wù)資源(或資源列表),例如博客,用戶時(shí),使用@PathVariable
這個(gè)是舉例是為了獲取具體某一個(gè)缺陷或者用戶的時(shí)候用
2、當(dāng)URL需要對資源或者資源列表進(jìn)行過濾,篩選時(shí),用@RequestParam
到此這篇關(guān)于Springboot中@RequestParam和@PathVariable的用法與區(qū)別詳解的文章就介紹到這了,更多相關(guān)@RequestParam和@PathVariable的用法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 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ū)別小結(jié)
相關(guān)文章
SpringMVC數(shù)據(jù)頁響應(yīng)ModelAndView實(shí)現(xiàn)頁面跳轉(zhuǎn)
本文主要介紹了SpringMVC數(shù)據(jù)頁響應(yīng)ModelAndView實(shí)現(xiàn)頁面跳轉(zhuǎn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
SpringBoot項(xiàng)目中resources文件讀取實(shí)踐
本文詳細(xì)介紹了SpringBoot項(xiàng)目中讀取resources目錄下文件的9種主流方式,并提供了一個(gè)完整的控制器Demo示例,幫助開發(fā)者快速定位最適合的資源加載方案2026-01-01
java使用POI讀取properties文件并寫到Excel的方法
這篇文章主要介紹了java使用POI讀取properties文件并寫到Excel的方法,涉及java操作properties文件及Excel文件的相關(guān)技巧,需要的朋友可以參考下2015-06-06
Java如何根據(jù)前端返回的字段名進(jìn)行查詢數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了Java如何根據(jù)前端返回的字段名進(jìn)行查詢數(shù)據(jù),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-11-11
SpringBoot自動(dòng)裝配Condition的實(shí)現(xiàn)方式
這篇文章主要介紹了SpringBoot自動(dòng)裝配Condition的實(shí)現(xiàn)方式,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08

