Spring中@PathVariable和@RequestParam注解的用法區(qū)別
前言
@PathVariable和@RequestParam的作用都是從請(qǐng)求里面獲取參數(shù),只是用法不同
- PathVariable:
- http://localhost:8080/testPathVariable/parm1/111111/parm2/222222
- RequestParam:
- http://localhost:8080/testRequestParam?id1=11111&id2=22222
@PathVariable
首先看一下代碼以及響應(yīng)結(jié)果:
請(qǐng)求路徑url://localhost:8080/testPathVariable/parm1/111111/parm2/222222
代碼:
@GetMapping("/testPathVariable/parm1/{id1}/parm2/{id2}") public String testPathVariable(@PathVariable(value = "id1") String id,@PathVariable String id2) { return "testPathVariable, param1:"+id+" , param2:"+id2; }
請(qǐng)求結(jié)果:
解析
@PathVariable用法: 請(qǐng)求路徑采用 “/ 參數(shù)” 向后臺(tái)傳入?yún)?shù) 后臺(tái)接收采用 {id1} 占位符形式的方式來(lái)接收請(qǐng)求參數(shù)
@PathVariable的參數(shù):
- name: 與請(qǐng)求的具體哪個(gè)參數(shù)做綁定
- required: 參數(shù)是否必須 true or false
- value: 跟name一樣的作用,與請(qǐng)求的具體哪個(gè)參數(shù)做綁定
@RequestParam
首先看一下代碼以及響應(yīng)結(jié)果:
請(qǐng)求路徑://localhost:8080/testRequestParam?id1=11111&id2=22222
代碼:
@GetMapping("/testRequestParam") public String testRequestParam(@RequestParam(value = "id1") String id, @RequestParam String id2) { return "testRequestParam, param1:"+id+" , param2:"+id2; }
請(qǐng)求結(jié)果:
解析
@RequestParam用法: 請(qǐng)求路徑采用的是形如 /testRequestParam?id1=11111&id2=22222 的形式
后臺(tái)接收用@RequestParam注解,用value屬性綁定參數(shù)接收
@RequestParam的參數(shù):
- name: 與請(qǐng)求的具體哪個(gè)參數(shù)做綁定
- required: 參數(shù)是否必須 true or false
- value: 跟name一樣的作用,與請(qǐng)求的具體哪個(gè)參數(shù)做綁定
- defaultValue:如果請(qǐng)求沒(méi)有攜帶這個(gè)參數(shù)或者參數(shù)為空,采用默認(rèn)值
異同點(diǎn)
相同點(diǎn):都是從請(qǐng)求里面獲取參數(shù)
不同點(diǎn):@RequestParam多了一個(gè)defaultValue屬性,用于處理請(qǐng)求沒(méi)有這個(gè)參數(shù)的情況賦予一個(gè)默認(rèn)值
@GetMapping("/testRequestParam") public String testRequestParam(@RequestParam(value = "id1",required = false, defaultValue = "paramdefault") String id, @RequestParam String id2) { return "testRequestParam, param1:"+id+" , param2:"+id2; }
到此這篇關(guān)于Spring中@PathVariable和@RequestParam注解的用法區(qū)別的文章就介紹到這了,更多相關(guān)@PathVariable和@RequestParam用法區(qū)別內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中文亂碼解決方案全解析,讓你的程序“說(shuō)人話”!
探索Java中文亂碼解決方案全解析,讓你的程序終于能“說(shuō)人話”!厭倦了看著一串串的問(wèn)號(hào)或者奇怪符號(hào)嗎?跟著我們的指南,一步步輕松解鎖中文亂碼的秘密,讓你的代碼清晰表達(dá)每一個(gè)字,需要的朋友可以參考下2024-02-02Springboot使用filter對(duì)response內(nèi)容進(jìn)行加密方式
這篇文章主要介紹了Springboot使用filter對(duì)response內(nèi)容進(jìn)行加密方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03springBoot 項(xiàng)目排除數(shù)據(jù)庫(kù)啟動(dòng)方式
這篇文章主要介紹了springBoot 項(xiàng)目排除數(shù)據(jù)庫(kù)啟動(dòng)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09idea?intellij快速修復(fù)if語(yǔ)句缺少大括號(hào)的問(wèn)題
這篇文章主要介紹了idea?intellij快速修復(fù)if語(yǔ)句缺少大括號(hào)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04SpringBoot中添加監(jiān)聽(tīng)器及創(chuàng)建線程的代碼示例
這篇文章主要介紹了SpringBoot中如何添加監(jiān)聽(tīng)器及創(chuàng)建線程,文中有詳細(xì)的代碼示例,具有一定的參考價(jià)值,需要的朋友可以參考下2023-06-06ZooKeeper集群操作及集群Master選舉搭建啟動(dòng)
這篇文章主要為大家介紹了ZooKeeper集群操作及集群Master選舉搭的建啟動(dòng)詳解,<BR>有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08詳解SpringBoot的三種緩存技術(shù)(Spring Cache、Layering Cache 框架、Alibaba J
這篇文章主要介紹了SpringBoot的三種緩存技術(shù),幫助大家更好的理解和學(xué)習(xí)springboot框架,感興趣的朋友可以了解下2020-10-10