How to Handle Multiple Path Variable in Spring Boot

How to Handle Multiple Path Variable in Spring Boot. In this String boot tutorial, we will see an Spring boot example to Handle Multiple Path Variables in Spring Boot.

What is @PathVariable in Spring boot

The @PathVariable annotation extracts values from the URI. It is often used with RESTFul web services. It has the following elements(optional):

  • name: name of the path variable.
  • required: It tells whether the path variable is required or not.
  • value: alias for name.

Syntax:

Handle Multiple Path Variables in Spring Boot

Create project new Spring boot project

Create a project using Spring Initializr. If you don’t know how to create a project using the same check this article https://codedec.com/tutorials/how-to-create-spring-boot-project-using-spring-initializr/

Import a Project

Now, it’s time to import the project into STS. Go to File > Import > Maven > Existing Maven Project > Next > Browse > Select the project > Finish. Now it will take time to import the project and will import all the dependencies in case you added.

Configure application. properties file

# change the port
server.port=8888

Create a Controller

Create a TestController to handle requests. The request for the web page will be handle by the handler methods in the controller.

package com.abc.demo.ctl;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

 @RequestMapping(path = "/student/{rollNo}/{class}")
 public String home(@PathVariable("rollNo") long rollNo,@PathVariable("class") long classNo) {
  
  return "Roll No is : "+rollNo+"\nClass is: "+classNo;
 }	
}
  • @Controller annotation marks the TestController class a Request Handler.
  • Every request coming for the ‘/student’ URL will be handled by the home() method.
  • Here, the @Pathvariable is used to extract rollNo and class value.

Run the Application

Now, Run the PathVariableExampleApplication and Go to localhost:8888/student/10/6 and see the following output.

Thus, this is How we extract values from the URI.

Now, consider a situation, where you have multiple path variables like 10 or more, Here, you won’t be writing @PathVariable annotation multiple times So, we have one more way of handling it is by using Map as shown below.

@RequestMapping(path = "/student2/{rollNo}/{class}")
 public String home1(@PathVariable Map<String, String> pathVariables) {
  String rollNo = pathVariables.get("rollNo");
  String classNo = pathVariables.get("class");
  return "<h1>Roll No is : "+rollNo+"\nClass is: "+classNo+"</h1>";
}

Here, map all path variables to the Map interface. Add this method to the TestController class and run the application once again and now go to localhost:8888/student2/10/6 and see the following output.

In this way, we learn How to access path variables using @PathVariable annotation.