使用@pathvariable與@requestparam碰到的一些問(wèn)題及解決
@pathvariable與@requestparam碰到的一些問(wèn)題
一、@pathvariable
可以將 URL 中占位符參數(shù)綁定到控制器處理方法的入?yún)⒅校篣RL 中的 {x} 占位符可以通過(guò)@PathVariable("x") 綁定到操作方法的入?yún)⒅小?/p>
@GetMapping("/test/{id}") public String test(@PathVariable("id") String id){ System.out.println("test:"+id); return SUCCESS; }
可以看出使用@pathvariable注解它直接從url中取參,但是如果參數(shù)是中文就會(huì)出現(xiàn)亂碼情況,這時(shí)應(yīng)該使用@requestparam注解
二、@requestparam
它是直接從請(qǐng)求中取參,它是直接拼接在url后面(demo?name=張三)
@GetMapping("/demo") public String test(@requestparam(value="name") String name){ System.out.println("test:"+name); return SUCCESS; }
注:如果參數(shù)不必須傳入的話(huà),我們從源碼中可以看出兩者required默認(rèn)為true,如圖:
所以我們可以這樣寫(xiě),只寫(xiě)一個(gè)例子
@GetMapping("/demo") public String test(@requestparam(value="name", required = false) String name){ System.out.println("test:"+name); return SUCCESS; }
@PathVariable和@RequestParam的使用說(shuō)明
要說(shuō)明@PathVariable和@RequestParam的使用,首先介紹 @RequestMapping
RequestMapping是一個(gè)用來(lái)處理請(qǐng)求地址映射的注解,可用于類(lèi)或方法上。用于類(lèi)上,表示類(lèi)中的所有響應(yīng)請(qǐng)求的方法都是以該地址作為父路徑。
RequestMapping:Annotation for mapping web requests onto methods in request-handling classes with flexible method signatures.Both Spring MVC and Spring WebFlux support this annotation.
RequestMapping注解有六個(gè)屬性,常用的是value,method;還有consumes,produces,params,headers。
value屬性:指定請(qǐng)求的實(shí)際地址,當(dāng)只設(shè)置value屬性時(shí),默認(rèn)省略不寫(xiě)
即:@RequestMapping("/hello")或@RequestMapping(value="/hello")
value的uri值為以下三類(lèi)
- A)可以指定為普通的具體值;
- B)可以指定為含有某變量的值(URI Template Patterns with Path Variables);
- C)可以指定為含正則表達(dá)式的值( URI Template Patterns with Regular Expressions)。
HelloController.java極簡(jiǎn)代碼示例,既有PathVariable也有RequestParam
@RestController public class HelloController { @RequestMapping("/hellopv/{name}") public String helloPV(@PathVariable String name, @RequestParam String username) { String hello = "Hello " + username + " [" + name + "] !"; return hello; } }
感性認(rèn)識(shí)一下,測(cè)試上述代碼http://cos6743:8081/hellopv/tom?username=YangTom
@PathVariable是處理requet uri template中variable 的注解,實(shí)現(xiàn)了url入?yún)⒔壎ǖ椒椒▍?shù)上。
即:可以獲取URL請(qǐng)求路徑中的變量值,比如:RequestMapping("/hellopv/{name}")中的name
@RequestParam獲取URL請(qǐng)求數(shù)據(jù),是常用來(lái)處理簡(jiǎn)單類(lèi)型的綁定注解。
通過(guò)Request.getParameter()獲取入?yún)?,故此可以處理url中的參數(shù),也可以處理表單提交的參數(shù)和上傳的文件。
拓展
handler method 參數(shù)綁定常用的注解,根據(jù)處理的Request的不同內(nèi)容分為四類(lèi)常用類(lèi)型
- A、處理requet uri 部分(指uri template中variable)的注解: @PathVariable;
- B、處理request header部分的注解: @RequestHeader, @CookieValue;
- C、處理request body部分的注解:@RequestParam, @RequestBody;
- D、處理attribute類(lèi)型是注解: @SessionAttributes, @ModelAttribute;
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
淺析java并發(fā)中的Synchronized關(guān)鍵詞
這篇文章主要介紹了java并發(fā)中的Synchronized關(guān)鍵詞,本文通過(guò)思路代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02使用redis的increment()方法實(shí)現(xiàn)計(jì)數(shù)器功能案例
這篇文章主要介紹了使用redis的increment()方法實(shí)現(xiàn)計(jì)數(shù)器功能案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11java打包maven啟動(dòng)報(bào)錯(cuò)jar中沒(méi)有主清單屬性
本文主要介紹了java打包maven啟動(dòng)報(bào)錯(cuò)jar中沒(méi)有主清單屬性,可能原因是創(chuàng)建springboot項(xiàng)目時(shí),自動(dòng)導(dǎo)入,下面就來(lái)介紹一下解決方法,感興趣的可以了解一下2024-03-03Java中IO流使用FileWriter寫(xiě)數(shù)據(jù)基本操作詳解
這篇文章主要介紹了Java中IO流FileWriter寫(xiě)數(shù)據(jù)操作,FileWriter類(lèi)提供了多種寫(xiě)入字符的方法,包括寫(xiě)入單個(gè)字符、寫(xiě)入字符數(shù)組和寫(xiě)入字符串等,它還提供了一些其他的方法,如刷新緩沖區(qū)、關(guān)閉文件等,需要的朋友可以參考下2023-10-10Springboot指定掃描路徑的實(shí)現(xiàn)示例
本文主要介紹了Springboot指定掃描路徑的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-05-05Jmeter?BeanShell?內(nèi)置變量vars、props、prev的使用詳解
這篇文章主要介紹了Jmeter?BeanShell?內(nèi)置變量vars、props、prev的使用?,文中給大家介紹了Jmeter中關(guān)于BeanShell的相關(guān)知識(shí),結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-10-10