欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Springboot中@RequestParam和@PathVariable的用法與區(qū)別詳解

 更新時間:2024年01月04日 10:39:27   作者:時間不會賴著不走  
這篇文章主要介紹了Springboot中@RequestParam和@PathVariable的用法與區(qū)別詳解,RESTful API設(shè)計的最佳實踐是使用路徑參數(shù)來標(biāo)識一個或多個特定資源,而使用查詢參數(shù)來對這些資源進行排序/過濾,需要的朋友可以參考下

@RequestParam和@PathVariable的用法

RESTful API設(shè)計的最佳實踐是使用路徑參數(shù)來標(biāo)識一個或多個特定資源,而使用查詢參數(shù)來對這些資源進行排序/過濾

@PathVariable

會用在單個對象的查詢上,比如要根據(jù)ID值查詢學(xué)生信息,就會在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

會用在組合查詢多個對象,比如跟據(jù)姓名模糊查詢和性別組合查詢篩選學(xué)生,就會發(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ù)資源(或資源列表),例如博客,用戶時,使用@PathVariable

這個是舉例是為了獲取具體某一個缺陷或者用戶的時候用

2、當(dāng)URL需要對資源或者資源列表進行過濾,篩選時,用@RequestParam

到此這篇關(guān)于Springboot中@RequestParam和@PathVariable的用法與區(qū)別詳解的文章就介紹到這了,更多相關(guān)@RequestParam和@PathVariable的用法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論