SpringMVC註解 @Requestparam 和 @PathVariable 小記
一、@Requestparam
- 作用:綁定前端請求參數到對應的Controller層中方法的形參上
- 註解參數:
1. value:參數名稱,請求參數名必須與value一致,否則參數不會自動映射
2. required:被標註參數是否必須包含;默認爲true,請求參數中必須包含被標註參數,否則會報錯,如果是false,請求參數可以不傳被標註參數,Boolean類型默認賦值爲false,其他類型默認賦值爲null;
3. defaultValue:設置默認參數值,如果設置了該值,required=true將失效,自動爲false,如果沒有傳該參數,就使用所設置的默認值;
- 示例:
@RequestMapping("/test1")
public void testRequestParam1(@RequestParam String hello){
}
@RequestMapping("/test2")
public void testRequestParam2(@RequestParam(value = "HELLO",required = false) String hello){
}
@RequestMapping("/test3")
public void testRequestParam3(@RequestParam(value = "HELLO", defaultValue = "flag") String hello){
}
二、@PathVariable
- 作用:將 URL中的佔位符參數{xxx}綁定到Controller控制器處理方法的形參中;
- 註解參數:
1. value:參數名稱,需要跟URL中佔位符名稱一致;
2. required:被標註參數是否必須包含;默認爲true,請求參數中必須包含被標註參數,否則會報錯,如果是false,請求參數可以不傳被標註參數,Boolean類型默認賦值爲false,其他類型默認賦值爲null(注意設置爲false,需要在@RequestMapping中配置多個請求路徑,示例Demo4);
- 示例:
1、示例1(原始註解@PathVariable )
- Demo1中URL中的佔位符名稱和Controller中方法形參一致,所以可以映射成功
- Demo1示例:
@RestController
public class TestController {
@RequestMapping("/test4/{hello}/{world}")
public String testPathVariable1(@PathVariable String hello, @PathVariable String world) {
String str = "@PathVariable 佔位符映射-hello:"+ hello+" world:"+world;
return str;
}
}
- Demo1結果:
2、示例2(URL佔位符名稱與Value名稱一致)
- Demo2中URL佔位符名稱與Controller中方法形參名稱不一致,所以需要在@PathVariable中去聲明(@PathVariable中聲明名稱必須與URL佔位符名稱一致)
- Demo2示例:
@RestController
public class TestController {
@RequestMapping("/test5/{abc}/{efg}")
public String testPathVariable2(@PathVariable("abc") String hello, @PathVariable("efg") String world) {
String str = "@PathVariable 佔位符映射-hello:"+ hello+" world:"+world;
return str;
}
}
- Demo2結果:
3、示例3(URL佔位符名稱與Value名稱不一致)
- Demo3中URL佔位符名稱與Controller中方法形參名稱不一致,並且@PathVariable中也沒有進行聲明,所以請求時會報錯;
- Demo3示例:
@RestController
public class TestController {
@RequestMapping("/test6/{abc}/{efg}")
public String testPathVariable3(@PathVariable String hello, @PathVariable String world) {
String str = "@PathVariable 佔位符映射-hello:"+ hello+" world:"+world;
return str;
}
}
- Demo3結果:
4、示例4(required = false)
- Demo4中主要是測試required = false時,配置多個請求映射路徑;
- Demo4示例:
@RestController
public class TestController {
@RequestMapping(value = {"/test7/{abc}/{efg}","/test7/{abc}","/test7"})
public String testPathVariable4(@PathVariable(value = "abc",required = false) String hello,
@PathVariable(value = "efg",required = false) String world) {
String str = "@PathVariable 佔位符映射-hello:"+ hello+" world:"+world;
return str;
}
}
- Demo4結果:
5、示例5(正則表達式規範請求URL)
- Demo5中主要測試在@RequestMapping()的URL中配置正則表達式規範請求參數格式;
- Demo5示例:
@RestController
public class TestController {
@RequestMapping(value = {"/test8/{abc:^[a-zA-Z]+$}/{efg}","/test8/{abc:^[a-zA-Z]+$}","/test8"})
public String testPathVariable5(@PathVariable(value = "abc",required = false) String hello,
@PathVariable(value = "efg",required = false) String world) {
String str = "@PathVariable 佔位符映射-hello:"+ hello+" world:"+world;
return str;
}
}
- Demo5結果: