Spring MVC Matrix Variables
Last Updated :
28 Apr, 2025
In this article, we will learn about Spring MVC Matrix Variables. Matrix Variable is used to parse and bind name-value pairs inside a path segment to method parameters. A semicolon separates many pairs. Matrix variables must first be enabled.
XML Matrix Variable
Below are the necessary dependencies and maven plugins added to the pom.xml file.
XML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>5.2.1</modelVersion>
<groupId>org.geeksforgeeks</groupId>
<artifactId>matrixvariableex</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<spring-version>5.3.23</spring-version>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
</plugin>
<plugin>
<groupId>org.geeksforgeeks</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.4.40.v20230714</version>
</plugin>
</plugins>
</build>
</project>
Matrix Variables in Spring MVC
These variables can exist anywhere along the route. Each matrix variable is delimited by a semicolon (';') and values are provided using the character equals ("="). We may either repeat the name of the variable or use the comma (',') to distinguish various values on the same route.
http://localhost:8080/spring-mvc-java-2/authorArea/workingArea=rh,informatics,admin
Using annotation @MatrixVariable
The @MatrixVariable annotation should be used in Spring MVC when referring to these variables.
public class Author {
private long id;
private String name;
private String contactNumber;
}
Handle Matrix Variable Properties
For the variable, we may set required or default properties. The contact number is necessary in the example below. The following procedure will be used to handle the request.
Java
@RequestMapping(value = "/authorsContacts/{contactNumber}",method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<List<Author>> getAuthorByContactNumber(
@MatrixVariable(required = true) String contactNumber) {
List<Author> authorsList = new ArrayList<Author>();
return new ResponseEntity<List<Author>>(authorsList, HttpStatus.OK);
}
Complement Path Variables
We can look for an employee by name, but we can also get the contact number starting with that person's name.The following should be the request for this search:Following is how the request will be handled.
Java
@RequestMapping(value = "/authors/{name}", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<List<Author>> getAuthorByNameAndBeginContactNumber(
@PathVariable String name, @MatrixVariable String beginContactNumber) {
List<Author> authorsList = new ArrayList<Author>();
return new ResponseEntity<>(authorsList, HttpStatus.OK);
}
Binding Matrix Variables
We can connect variables to a map if, for whatever reason, we wish to get every variable that is accessible on the path.
Java
@GetMapping("authorData/{author}")
@ResponseBody
public ResponseEntity<Map<String, String>> getAuthorData(
@MatrixVariable Map<String, String> matrixVars) {
return new ResponseEntity<>(matrixVars, HttpStatus.OK);
}
We should use the following as an input parameter as we only want to retrieve all the variables that are a part of authorData:
Java
@RequestMapping(
value = "/orgAuthor/{org}/authorData/{author}",
method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<Map<String, String>> getAuthorDataFromOrg(
@MatrixVariable(pathVar = "author") Map<String, String> matrixVars) {
//
}
Partial Binding
Matrix variables have the benefit of versatility in addition to simplicity, since they may be applied in several contexts. Using the following as an input parameter is appropriate if all we want to know is the name of the companyData segment's matrix variable:
@MatrixVariable(value="name", pathVar="organization") String name
Similar Reads
Spring - Variable in SpEL EvaluationContext interface is implemented by the StandardEvaluationContext class. To resolve properties or methods, it employs a reflection technique. The method setVariable on the StandardEvaluationContext can be used to set a variable. Using the notation #variableName, we may utilize this variabl
3 min read
Spring MVC Tutorial In this tutorial, we'll cover the fundamentals of Spring MVC, including setting up your development environment, understanding the MVC architecture, handling requests and responses, managing forms, and integrating with databases. You'll learn how to create dynamic web pages, handle user input, and i
7 min read
Variable Names in MATLAB A variable is a named-memory location that stores different types of data which can be used to perform a specific set of operations. It can be thought of as a container that holds some value in memory. The Matlab workspace store all the variables being used during a session. This workspace not only
5 min read
Spring MVC - WebMvcConfigure Spring WebMvcConfigurer enables developers to customize the default MVC setups in Spring applications. By implementing this interface, users can customize several features of the MVC framework, such as interceptors, resource handling, and view controllers. In this article, we will learn how to Custo
4 min read
Spring vs Spring Boot vs Spring MVC Are you ready to dive into the exciting world of Java development? Whether you're a seasoned pro or just starting out, this article is your gateway to mastering the top frameworks and technologies in Java development. We'll explore the Spring framework, known for its versatility and lightweight natu
8 min read
Spring Tutorial Spring Framework is a comprehensive and versatile platform for enterprise Java development. It is known for its Inversion of Control (IoC) and Dependency Injection (DI) capabilities that simplify creating modular and testable applications. Key features include Spring MVC for web development, Spring
13 min read