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} 占位符形式的方式來接收請(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)求沒有攜帶這個(gè)參數(shù)或者參數(shù)為空,采用默認(rèn)值
異同點(diǎn)
相同點(diǎn):都是從請(qǐng)求里面獲取參數(shù)
不同點(diǎn):@RequestParam多了一個(gè)defaultValue屬性,用于處理請(qǐng)求沒有這個(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)文章希望大家以后多多支持腳本之家!
- springboot中@RequestParam和@PathVariable區(qū)別
- SpringBoot中@PathVariable、@RequestParam和@RequestBody的區(qū)別和使用詳解
- Spring中@RequestParam、@RequestBody和@PathVariable的用法詳解
- Springboot中@RequestParam和@PathVariable的用法與區(qū)別詳解
- @PathVariable、@RequestParam和@RequestBody的區(qū)別
- 方法參數(shù)屬性params,@PathVariable和@RequestParam用法及區(qū)別
- @PathVariable和@RequestParam傳參為空問題及解決
- 使用@pathvariable與@requestparam碰到的一些問題及解決
- 聊聊@RequestParam,@PathParam,@PathVariable等注解的區(qū)別
- Java中@PathVariable 和 @RequestParam的區(qū)別小結(jié)
相關(guān)文章
SpringBoot integration實(shí)現(xiàn)分布式鎖的示例詳解
常規(guī)項(xiàng)目都是采用Redission來實(shí)現(xiàn)分布式鎖,進(jìn)行分布式系統(tǒng)中資源競爭加鎖操作,偶然發(fā)現(xiàn)SpringBoot中的integration也實(shí)現(xiàn)多種載體的分布式鎖控制,下面我們就來看看具體實(shí)現(xiàn)方法吧2023-12-12
springboot+Vue實(shí)現(xiàn)分頁的示例代碼
本文主要介紹了springboot+Vue實(shí)現(xiàn)分頁的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06
Spring Boot 自定義 Shiro 過濾器無法使用 @Autowired問題及解決方法
這篇文章主要介紹了Spring Boot 自定義 Shiro 過濾器無法使用 @Autowired問題及解決方法 ,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06
Java實(shí)現(xiàn)文件切割拼接的實(shí)現(xiàn)代碼
這篇文章主要介紹了Java實(shí)現(xiàn)文件切割拼接的實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11

