Skip to content
Advertisement

How does pathVar Attribute of @MatrixVariable annotation works in Spring?

I was reading regarding the @Matrixvariable annotation in Spring doc Spring Doc

I have Understood this simple syntax // GET /pets/42;q=11;r=22

JavaScript

but having problem in understanding the below snippet

JavaScript

What is this syntax @MatrixVariable(pathVar=”petId””) I haven’t understood the pathVar attribute of Matrixvariable annotation?

This line is ok for me // matrixVars: ["q" : [11,22], "r" : 12, "s" : 23] that this variable added with all the matrix variables. but how does petMatrixVars added with these Specific values mean

JavaScript

Thanks in Advance for your time spent on this thread!!

Advertisement

Answer

This is called Partial Binding it is used to get all variables from that segment in that path or if you want to get each variable from that path segment docs, and output is wrong in this documentation here

In your example you will get all variables that are in path after petId {21}

JavaScript

If you want to get only q after petId segment then

JavaScript

Here is the example with output, for @MatrixVariable we need to enable them first

JavaScript

Controller with @requestmapping method

JavaScript

Request: http://localhost:8080/sample/owners/42;q=11;r=12/pets/21;q=22;s=23

Output:

JavaScript

And if i change @MatrixVariable Map<String, List<String>> matrixVars, the output is

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement