The URL can have name-value pairs within its path segment. To map these pairs correctly Matrix Variables can be used in Spring MVC. Lets see some of the Spring MVC Matrix Variable Examples and how we can configure these variables in our app.
1. Configuration
For using the Matrix Variables you need to enable it first. You can do that in 2 ways
a. Set the removeSemicolonContent property of RequestMappingHandlerMapping to false. By default it is set to true.
b. In the MVC namespace, the element has an enable-matrix-variables attribute that should be set to true. By default it is set to false.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.kscodes.sampleproject" /> <mvc:annotation-driven enable-matrix-variables="true"/> </beans> |
2. Simple Matrix Variable using @MatrixVariable
Matrix Variables are separated with ; semicolon.
e.g.
/andriod;name=marshmallow;version=6.0.2
Multiple variables can be separated with , commaor repeating the variable name.
e.g.
1 2 |
/andriod;name=lollipop,marshmallow /andriod;name=lollipop;name=marshmallow |
These variables can be mapped to a method param using @MatrixVariable
1 2 3 4 5 6 7 8 9 10 11 12 |
@RequestMapping("/os/{type}") public String osDetails(@PathVariable String type, @MatrixVariable String name, @MatrixVariable String version) { // Your Implementation } /os/android;name=marshmallow;version=6.0.2 type = android name = marshmallow version = 6.0.2 |
3. Matrix Variable Sequence
Matrix variables can be present in any part of the URL, so to determine which matrix variable maps to which element we use value and pathVar attributes
1 |
@MatrixVariable(name="i", pathVar="name") |
example
1 2 3 4 5 6 7 8 9 10 11 |
@RequestMapping("/employee/{empName}/dept/{deptName}") public String empDetails(@MatrixVariable(value = "id", pathVar = "empName") int empId, @MatrixVariable(value = "id", pathVar = "deptName") int deptId) { // Your Implementation } // http://localhost:8080/sampleproject/employee/Steve;id=101/dept/Sales;id=2 // empId = 101 // deptId = 2 |
4. Matrix Variable Optional and Default value attributes
Matrix can be optional by setting the required attribute to false. We can also provide default values to them using the defaultValue attribute
1 |
@MatrixVariable(required=false, defaultValue="1") int id |
5. Map of Matrix Variable
Unlike the other variables , Matrix variables can also be obtained in a map.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
@RequestMapping("employee/{empName}/dept/{deptName}") public String empDetails( @MatrixVariable Map<String, String> allMatrixVars, @MatrixVariable(pathVar="empName") Map<String, String> empMatrixVar, @MatrixVariable(pathVar="deptName") Map<String, String> deptMatrixVar) { // Your Implementation } // employee/Steve;id=101;salary=2500/dept/Sales;id=2;type=internal // Output // allMatrixVars [ id=[101,2],salary=2500,type=internal ] // empMatrixVar [ id=101,salary=2500 ] // deptMatrixVar [ id=2,type=internal ] |
Hope you liked the Spring MVC Matrix Variable Examples.