淺談SpringBoot處理url中的參數(shù)的注解
1.介紹幾種如何處理url中的參數(shù)的注解
@PathVaribale 獲取url中的數(shù)據(jù)
@RequestParam 獲取請(qǐng)求參數(shù)的值
@GetMapping 組合注解,是 @RequestMapping(method = RequestMethod.GET) 的縮寫(xiě)
(1)PathVaribale 獲取url中的數(shù)據(jù)
看一個(gè)例子,如果我們需要獲取Url=localhost:8080/hello/id中的id值,實(shí)現(xiàn)代碼如下:
@RestController
public class HelloController {
@RequestMapping(value="/hello/{id}/{name}",method= RequestMethod.GET)
public String sayHello(@PathVariable("id") Integer id,@PathVariable("name") String name){
return "id:"+id+" name:"+name;
}
}
在瀏覽器中 輸入地址: localhost:8080/hello/100/helloworld 然后會(huì)在html頁(yè)面上打印出:
id:81
同樣,如果我們需要在url有多個(gè)參數(shù)需要獲取,則如下代碼所示來(lái)做就可以了。
@RestController
public class HelloController {
@RequestMapping(value="/hello/{id}/{name}",method= RequestMethod.GET)
public String sayHello(@PathVariable("id") Integer id,@PathVariable("name") String name){
return "id:"+id+" name:"+name;
}
}
在瀏覽器中輸入地址: localhost:8080/hello/100/helloworld 然后會(huì)在html頁(yè)面上打印出:
id:100 name:helloworld
以上,通過(guò) @PathVariable 注解來(lái)獲取URL中的參數(shù)時(shí)的前提條件是我們知道url的格式時(shí)怎么樣的。
只有知道url的格式,我們才能在指定的方法上通過(guò)相同的格式獲取相應(yīng)位置的參數(shù)值。
一般情況下,url的格式為: localhost:8080/hello?id=98 ,這種情況下該如何來(lái)獲取其id值呢,這就需要借助于 @RequestParam 來(lái)完成了
2.@RequestParam 獲取請(qǐng)求參數(shù)的值
例如:
@RestController
public class HelloController {
@RequestMapping(value="/hello",method= RequestMethod.GET)
public String sayHello(@RequestParam("id") Integer id){
return "id:"+id;
}
}
在瀏覽器中輸入地址: localhost:8080/hello?id=1000 ,可以看到如下的結(jié)果:
id:1000
當(dāng)我們?cè)跒g覽器中輸入地址: localhost:8080/hello?id ,即不輸入id的具體值,此時(shí)返回的結(jié)果為null。具體測(cè)試結(jié)果如下:
id:null
但是,當(dāng)我們?cè)跒g覽器中輸入地址: localhost:8080/hello ,即不輸入id參數(shù),則會(huì)報(bào)如下錯(cuò)誤:
whitelable Error Page錯(cuò)誤
@RequestParam 注解給我們提供了這種解決方案,即允許用戶(hù)不輸入id時(shí),使用默認(rèn)值,具體代碼如下:
@RestController
public class HelloController {
@RequestMapping(value="/hello",method= RequestMethod.GET)
//required=false 表示url中可以不穿入id參數(shù),此時(shí)就使用默認(rèn)參數(shù)
public String sayHello(@RequestParam(value="id",required = false,defaultValue = "1") Integer id){
return "id:"+id;
}
}
如果在url中有多個(gè)參數(shù),即類(lèi)似于 localhost:8080/hello?id=98&&name=helloworld 這樣的url,同樣可以這樣來(lái)處理。具體代碼如下:
@RestController
public class HelloController {
@RequestMapping(value="/hello",method= RequestMethod.GET)
public String sayHello(@RequestParam("id") Integer id,@RequestParam("name") String name){
return "id:"+id+ " name:"+name;
}
}
在瀏覽器中的測(cè)試結(jié)果如下: localhost:8080/hello?id=1000&name=helloworld 地址,就會(huì)顯示下面的內(nèi)容:
id:1000 name:helloworld
3.@GetMapping 組合注解
@GetMapping 是一個(gè)組合注解,是 @RequestMapping(method = RequestMethod.GET) 的縮寫(xiě)。該注解將HTTP Get 映射到 特定的處理方法上。
即可以使用 @GetMapping(value = “/hello”) 來(lái)代替 @RequestMapping(value=”/hello”,method= RequestMethod.GET) 。即可以讓我們精簡(jiǎn)代碼。
@RestController
public class HelloController {
//@RequestMapping(value="/hello",method= RequestMethod.GET)
@GetMapping(value = "/hello")
//required=false 表示url中可以不穿入id參數(shù),此時(shí)就使用默認(rèn)參數(shù)
public String sayHello(@RequestParam(value="id",required = false,defaultValue = "1") Integer id){
return "id:"+id;
}
}
4.PostMapping組合注解:
方法同GetMapping
以上這篇淺談SpringBoot處理url中的參數(shù)的注解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java8新特性O(shè)ptional類(lèi)及新時(shí)間日期API示例詳解
這篇文章主要為大家介紹了Java8新特性O(shè)ptional類(lèi)及新時(shí)間日期API示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
java代碼實(shí)現(xiàn)mysql分表操作(用戶(hù)行為記錄)
這篇文章主要介紹了java代碼實(shí)現(xiàn)mysql分表操作(用戶(hù)行為記錄),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02
第一次使用Android Studio時(shí)你應(yīng)該知道的一切配置(推薦)
這篇文章主要介紹了第一次使用Android Studio時(shí)你應(yīng)該知道的一切配置(推薦) ,需要的朋友可以參考下2017-09-09
一篇文章帶你解決 IDEA 每次新建項(xiàng)目 maven home directory 總是改變的問(wèn)題
這篇文章主要介紹了一篇文章帶你解決 IDEA 每次新建項(xiàng)目 maven home directory 總是改變的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
Java Scanner對(duì)象中hasNext()與next()方法的使用
這篇文章主要介紹了Java Scanner對(duì)象中hasNext()與next()方法的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
Mybatis動(dòng)態(tài)SQL實(shí)例詳解
這篇文章主要給大家介紹了關(guān)于Mybatis動(dòng)態(tài)SQL的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
maven+阿里云創(chuàng)建國(guó)內(nèi)鏡像的中央倉(cāng)庫(kù)(親測(cè)可用)
本篇文章主要介紹了maven+阿里云創(chuàng)建國(guó)內(nèi)鏡像的中央倉(cāng)庫(kù)(親測(cè)可用),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
Java實(shí)現(xiàn)視頻時(shí)間維度剪切的工具類(lèi)
這篇文章主要為大家詳細(xì)介紹了將視頻按照時(shí)間維度進(jìn)行剪切的Java工具類(lèi),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12

