spring4.0新特性,@matrix矩阵变量

RFC3986定义了在URI中包含name-value的规范!这也是spring4.0众多吸引人的新特性之一。先来举个小例子: java

好比URI是这样的: spring

//GET /pets/42;q=11;r=22 app

对应的方法定义则是: ui


@RequestMapping(value = "pets/{petId}, method = RequestMethod.GET)
public void findPet(@PathVariable String petId,@MatrixVariable int q){
...
}



相应的petId和q都会被映射到这个方法,另外注意若是匹配不到,则会报“bad request”,而且若是你的URI只是“/pets/42”的话也是能够映射到这个方法的,只是要把设置为@MatrixVariable(required =false)还有一种方式就是:


好比你的URI是: spa

//GET /owners/42;q=11/pets/21;q=22 code

@RequestMapping(value = "/owners/{ownerId/pets/{petId}", method = RequestMethod.GET)

public void findPet(
    @MatrixVariable(value = "q",pathVar="ownerId") int q1,
    @MatrixVariable(value = "q",pathVar="petId") int q2)
}

此处q1=11,q2=22. class

固然也能够使用Map做为方法入参: require

好比你的URI为: request

//GET /owners/42;q=11;r=12/pets/21;q=22;s=23 方法

对应方法为:

@RequestMapping(value = "/owners/{ownerId}/pets/{petId}", method = RequestMethod.GET)
public void findPet(
         @MatrixVariable Map<String,String> matrixVars,
         @MatrixVariable(pathVar = "petId") Map<String,String> petMatrixVars){
...
}



那么此处:

matrixVars = ["q" : [11,22], "r" : 12, "s" : 23]

petMatrixVars = ["q" : 11,"s" : 23]

以上例子来自reference,因此想深刻了解spring 4.0或者javaconfig或者spring boot等玩意,去看reference吧!

相关文章
相关标签/搜索