日常提交
This commit is contained in:
@@ -181,5 +181,100 @@ public void findPet(@PathVariable String petId) {
|
||||
```shell
|
||||
/cars;color=red,green;year=2012
|
||||
```
|
||||
当URL中预期含有Matrix变量时,被映射方法的URI中必须含有一个URI Variable({var})来覆盖该Matrix变量(即通过{var}来捕获URL中的Matrix变量),以此确保URL能被正确的映射。例子如下所示
|
||||
```java
|
||||
// GET /pets/42;q=11;r=22
|
||||
|
||||
@GetMapping("/pets/{petId}")
|
||||
public void findPet(@PathVariable String petId, @MatrixVariable int q) {
|
||||
|
||||
// petId == 42
|
||||
// q == 11
|
||||
}
|
||||
```
|
||||
在所有的path segment中都有可能含有matrix variable,如果在不同path segment中含有相同名称的matrix variable,可以通过如下方式进行区分:
|
||||
```java
|
||||
// GET /owners/42;q=11/pets/21;q=22
|
||||
|
||||
@GetMapping("/owners/{ownerId}/pets/{petId}")
|
||||
public void findPet(
|
||||
@MatrixVariable(name="q", pathVar="ownerId") int q1,
|
||||
@MatrixVariable(name="q", pathVar="petId") int q2) {
|
||||
|
||||
// q1 == 11
|
||||
// q2 == 22
|
||||
}
|
||||
```
|
||||
matrix variable参数也可以被设置为可选的,并且也能为其指派一个默认值:
|
||||
```java
|
||||
// GET /pets/42
|
||||
|
||||
@GetMapping("/pets/{petId}")
|
||||
public void findPet(@MatrixVariable(required=false, defaultValue="1") int q) {
|
||||
|
||||
// q == 1
|
||||
}
|
||||
```
|
||||
如果要获取所有的Matrix Variable并且将其缓存到一个map中,可以通过如下方式
|
||||
```java
|
||||
// GET /owners/42;q=11;r=12/pets/21;q=22;s=23
|
||||
|
||||
@GetMapping("/owners/{ownerId}/pets/{petId}")
|
||||
public void findPet(
|
||||
@MatrixVariable MultiValueMap<String, String> matrixVars,
|
||||
@MatrixVariable(pathVar="petId") MultiValueMap<String, String> petMatrixVars) {
|
||||
|
||||
// matrixVars: ["q" : [11,22], "r" : 12, "s" : 23]
|
||||
// petMatrixVars: ["q" : 22, "s" : 23]
|
||||
}
|
||||
```
|
||||
#### @RequestParam
|
||||
@RequestParam注解用于将http request中的param赋值给handler method的参数,使用方法如下:
|
||||
```java
|
||||
@GetMapping
|
||||
public String setupForm(@RequestParam("petId") int petId, Model model) {
|
||||
Pet pet = this.clinic.loadPet(petId);
|
||||
model.addAttribute("pet", pet);
|
||||
return "petForm";
|
||||
}
|
||||
```
|
||||
当@RequestParam注解的参数类型不是String时,类型转换会被自动的执行。
|
||||
将@RequestParam注解的参数类型指定为list或array时,会将具有相同param name的多个param value解析到其中。
|
||||
可以用@RequestParam来注解一个Map<String,String>或MultiValValue<String,String>类型,不要指定@RequestParam属性,此时,map将会被自动注入。
|
||||
#### @RequestHeader
|
||||
可以通过@RequestHeader注解来将http header值赋值给handler method参数。
|
||||
```shell
|
||||
# http header
|
||||
Host localhost:8080
|
||||
Accept text/html,application/xhtml+xml,application/xml;q=0.9
|
||||
Accept-Language fr,en-gb;q=0.7,en;q=0.3
|
||||
Accept-Encoding gzip,deflate
|
||||
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
|
||||
Keep-Alive 300
|
||||
```
|
||||
可以通过如下方式来获取header中的值:
|
||||
```java
|
||||
@GetMapping("/demo")
|
||||
public void handle(
|
||||
@RequestHeader("Accept-Encoding") String encoding,
|
||||
@RequestHeader("Keep-Alive") long keepAlive) {
|
||||
//...
|
||||
}
|
||||
```
|
||||
同样的,如果被注解参数的类型不是String,类型转换将会被自动执行。
|
||||
同样,也可以用@RequestHeader注解来标注Map<String,String>或MultiValueMap<String,String>或HttpHeaders类型参数,map会自动被header name和header value注入。
|
||||
#### @CookieValue
|
||||
可以通过@CookieValue注解将Http Cookie赋值给handler method参数,如果存在如下Cookie:
|
||||
```cookie
|
||||
JSESSIONID=415A4AC178C59DACE0B2C9CA727CDD84
|
||||
```
|
||||
可以通过如下方式来获取cookie的值
|
||||
```java
|
||||
@GetMapping("/demo")
|
||||
public void handle(@CookieValue("JSESSIONID") String cookie) {
|
||||
//...
|
||||
}
|
||||
```
|
||||
如果注解标注参数的类型不是String,会自动执行类型转换。
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user