Open In App

Spring MVC Matrix Variables

Last Updated : 05 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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"?>
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
    <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


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads