欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

方法參數(shù)屬性params,@PathVariable和@RequestParam用法及區(qū)別

 更新時(shí)間:2022年10月24日 14:17:06   作者:流煙默  
這篇文章主要介紹了方法參數(shù)屬性params,@PathVariable和@RequestParam用法及區(qū)別說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

【1】params

您可以根據(jù)請(qǐng)求參數(shù)條件縮小請(qǐng)求映射范圍。

您可以測試是否存在請(qǐng)求參數(shù)(myParam),是否缺少一個(gè)(!myParam),或針對(duì)特定值(myParam=myValue)。

以下示例顯示了如何測試特定值:

@GetMapping(path = "/pets/{petId}", params = "myParam=myValue")?
public void findPet(@PathVariable String petId) {
? ? // ...
}

您還可以將其用于請(qǐng)求頭條件,如下例所示:

@GetMapping(path = "/pets", headers = "myHeader=myValue")?
public void findPet(@PathVariable String petId) {
? ? // ...
}

簡而言之,params是指定request中必須包含某些參數(shù)值時(shí),才讓該方法處理。

@RequestMapping(value = "testParamsAndHeaders", params = { "username","age!=10" })
public String testParamsAndHeaders() {
? ? System.out.println("testParamsAndHeaders");
? ? return SUCCESS;
}

params 只是判斷url 或者 form data 中的參數(shù)是否復(fù)合params的定義,并不會(huì)直接綁定數(shù)據(jù)到方法的參數(shù)中,起到一個(gè)請(qǐng)求過濾作用。

【2】@PathVariable綁定URL中變量

綁定路徑中的占位符參數(shù)到方法參數(shù)變量中,只能綁定路徑中的占位符參數(shù),且路徑中必須有參數(shù)。無論是 GET 或者POST 只要 URL中有參數(shù)即可!

① 前臺(tái)實(shí)例

  • GET實(shí)例
Request URL:http://localhost:8080/SpringMVC-1/springmvc/testPathVariable/1
  • POST 實(shí)例
<form action="springmvc/testPathVariable/1" method="POST">
?? ?<input type="text" name="username" value=""/>
?? ?<input type="text" name="age" value=""/>
?? ?<input type="text" name="sex" value=""/>
?? ?<input type="submit" value="submit"/>
</form>

【注意】如果URL中無參數(shù),將會(huì)出錯(cuò);如果URL有參數(shù),但是沒有使用@PathVariabl該注解,那么URL的參數(shù)不會(huì)默認(rèn)與方法參數(shù)綁定!方法里的參數(shù)會(huì)默認(rèn)綁定表單里面對(duì)應(yīng)的參數(shù)!

② 后臺(tái)代碼

如果參數(shù)名與占位符一致,則可直接使用@PathVariable。如果不一致,則在@PathVariable( )括號(hào)內(nèi)綁定占位符。

@RequestMapping("/testPathVariable/{id}")
public String testPathVariable(@PathVariable("id") Integer id2) {
?? ?System.out.println("testPathVariable: " + id2);
?? ?return SUCCESS;
}

③ 注解源碼

@PathVariable 注解指示方法參數(shù)應(yīng)綁定到URI模板變量。支持@RequestMapping注解的處理程序方法。

如果方法參數(shù)類型為java.util.Map Map<String,String>,該方法參數(shù)將會(huì)被所有路徑變量name-value填充。

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PathVariable {
?? ?@AliasFor("name")
?? ?String value() default "";
?? ? //路徑變量綁定的名稱
?? ?@AliasFor("value")
?? ?String name() default "";
?? ?
?? ? //路徑變量是否必須。默認(rèn)為true,當(dāng)路徑變量不存在時(shí)拋出異常。
?? ? //如果當(dāng)路徑變量不存在時(shí),你傾向賦予null或者Java 8的Optional,請(qǐng)?jiān)O(shè)置為false,
?? ? //例如,在一個(gè)@ModelAttribute注解的方法處理不同的請(qǐng)求時(shí)。
?? ?boolean required() default true;

【3】@RequestParam

@RequestParam注解指示方法參數(shù)應(yīng)綁定web請(qǐng)求參數(shù)。

Spring MVC和Spring WebFlux中的注解處理程序方法支持如下:

在SpringMVC中,“request parameters”映射查詢參數(shù)、表單數(shù)據(jù)和multipart requests中的參數(shù)。這是因?yàn)镾ervletAPI將查詢參數(shù)和表單數(shù)據(jù)組合到一個(gè)a single map called "parameters",其中包括請(qǐng)求body的自動(dòng)解析。

在Spring WebFlux中,“request parameters”只映射到查詢參數(shù)。要在三部分起作用(query parameters, form data,and parts in multipart requests),可以使用數(shù)據(jù)綁定到用@ModelAttribute注解的命令對(duì)象。

如果方法參數(shù)類型為Map,并且指定了請(qǐng)求參數(shù)名稱,則假設(shè)有適當(dāng)?shù)霓D(zhuǎn)換器可用,則請(qǐng)求參數(shù)值將轉(zhuǎn)換為Map。

如果方法參數(shù)為java.util.Map Map<String,String>或org.springframework.util.MultiValueMap MultiValueMap<String,String>,如果未指定參數(shù)名,則方法參數(shù)Map會(huì)被所有的請(qǐng)求參數(shù)名-值填充。

① 注解源碼

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestParam {
?? ?@AliasFor("name")
?? ?String value() default "";
?? ?//請(qǐng)求參數(shù)要綁定的名稱
?? ?@AliasFor("value")
?? ?String name() default "";
/
?? ? //請(qǐng)求參數(shù)是否必須,默認(rèn)為true。如果參數(shù)不存在,則拋出異常。
?? ? //你可以選擇設(shè)置為false或者給予默認(rèn)值。
?? ?boolean required() default true;
?? ? //當(dāng)請(qǐng)求參數(shù)不存在或?yàn)榭諘r(shí),使用默認(rèn)值。提供一個(gè)默認(rèn)值將會(huì)隱形設(shè)置required屬性為false。
?? ?String defaultValue() default ValueConstants.DEFAULT_NONE;
}

② 前臺(tái)請(qǐng)求實(shí)例

  • GET
<a href="springmvc/testRequestParam?userName=tom&age=11&sex=boy" rel="external nofollow" >
  • POST
<form action="springmvc/testRequestParam" method="POST">
?? ?<input type="text" name="userName" value=""/>
?? ?<input type="text" name="age" value=""/>
?? ?<input type="text" name="sex" value=""/>
?? ?<input type="submit" value="submit"/>
</form>

注意 :

  • GET中的參數(shù)形式為:username=tom&age=11&sex=boy
  • POST中的參數(shù)形式為:以鍵值對(duì)形式保存在form data

③ 后臺(tái)代碼

@RequestMapping(value="/regist",produces="application/json;charset=utf-8")
@ResponseBody
public String regist(SysUser sysUser , @RequestParam(required=true,name="sex") String sex){
?? ?String userName = sysUser.getUserName();
?? ?String age = sysUser.getAge();
?? ?//...
?? ?return "regist success";
}

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論