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
MATLAB - Variables Prerequisite: Getting Started with MATLAB A variable in simple terms is a storage place that has some memory allocated to it. Basically, a variable used to store some form of data. Different types of variables require different amounts of memory and have some specific set of operations that can be a
3 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 @Value Annotation with Example The @Value annotation in Spring is one of the most important annotations. It is used to assign default values to variables and method arguments. It allows us to inject values from spring environment variables, system variables, and properties files. It also supports Spring Expression Language (SpEL)
6 min read