国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

淺談SpringBoot處理url中的參數的注解

2024-07-13 10:16:05
字體:
來源:轉載
供稿:網友

1.介紹幾種如何處理url中的參數的注解

@PathVaribale 獲取url中的數據

@RequestParam 獲取請求參數的值

@GetMapping 組合注解,是 @RequestMapping(method = RequestMethod.GET) 的縮寫

(1)PathVaribale 獲取url中的數據

看一個例子,如果我們需要獲取Url=localhost:8080/hello/id中的id值,實現代碼如下:

@RestControllerpublic 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 然后會在html頁面上打印出:

id:81

同樣,如果我們需要在url有多個參數需要獲取,則如下代碼所示來做就可以了。

@RestControllerpublic 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 然后會在html頁面上打印出:

id:100 name:helloworld

以上,通過 @PathVariable 注解來獲取URL中的參數時的前提條件是我們知道url的格式時怎么樣的。

只有知道url的格式,我們才能在指定的方法上通過相同的格式獲取相應位置的參數值。

一般情況下,url的格式為: localhost:8080/hello?id=98 ,這種情況下該如何來獲取其id值呢,這就需要借助于 @RequestParam 來完成了

2.@RequestParam 獲取請求參數的值

例如:

@RestControllerpublic class HelloController { @RequestMapping(value="/hello",method= RequestMethod.GET) public String sayHello(@RequestParam("id") Integer id){ return "id:"+id; }}

在瀏覽器中輸入地址: localhost:8080/hello?id=1000 ,可以看到如下的結果:

id:1000

當我們在瀏覽器中輸入地址: localhost:8080/hello?id ,即不輸入id的具體值,此時返回的結果為null。具體測試結果如下:

id:null

但是,當我們在瀏覽器中輸入地址: localhost:8080/hello ,即不輸入id參數,則會報如下錯誤:

whitelable Error Page錯誤

@RequestParam 注解給我們提供了這種解決方案,即允許用戶不輸入id時,使用默認值,具體代碼如下:

@RestControllerpublic class HelloController { @RequestMapping(value="/hello",method= RequestMethod.GET) //required=false 表示url中可以不穿入id參數,此時就使用默認參數 public String sayHello(@RequestParam(value="id",required = false,defaultValue = "1") Integer id){ return "id:"+id; }}

如果在url中有多個參數,即類似于 localhost:8080/hello?id=98&&name=helloworld 這樣的url,同樣可以這樣來處理。具體代碼如下:

@RestControllerpublic class HelloController { @RequestMapping(value="/hello",method= RequestMethod.GET) public String sayHello(@RequestParam("id") Integer id,@RequestParam("name") String name){ return "id:"+id+ " name:"+name; }}

在瀏覽器中的測試結果如下: localhost:8080/hello?id=1000&name=helloworld 地址,就會顯示下面的內容:

id:1000 name:helloworld

3.@GetMapping 組合注解

@GetMapping 是一個組合注解,是 @RequestMapping(method = RequestMethod.GET) 的縮寫。該注解將HTTP Get 映射到 特定的處理方法上。

即可以使用 @GetMapping(value = “/hello”) 來代替 @RequestMapping(value=”/hello”,method= RequestMethod.GET) 。即可以讓我們精簡代碼。

@RestControllerpublic class HelloController {//@RequestMapping(value="/hello",method= RequestMethod.GET)@GetMapping(value = "/hello")//required=false 表示url中可以不穿入id參數,此時就使用默認參數public String sayHello(@RequestParam(value="id",required = false,defaultValue = "1") Integer id){ return "id:"+id; } }

4.PostMapping組合注解:

方法同GetMapping

以上這篇淺談SpringBoot處理url中的參數的注解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 广平县| 土默特右旗| 介休市| 衡山县| 鄂州市| 赣州市| 汉沽区| 石门县| 承德县| 南雄市| 东阳市| 赤壁市| 正阳县| 慈溪市| 察隅县| 霍城县| 余姚市| 安吉县| 太仓市| 兰坪| 镇平县| 石台县| 奉贤区| 赤城县| 柞水县| 松江区| 明光市| 嘉峪关市| 上虞市| 睢宁县| 历史| 东至县| 萝北县| 淮阳县| 全椒县| 囊谦县| 和龙市| 周宁县| 什邡市| 阳新县| 阳新县|