Spring boot @PathVariable annotation Example

Hey guys in this post we will discuss about the Spring boot @PathVariable annotation and their example in detail.


Spring boot @PathVariable annotation used on a method argument to bind it to the value of a URI template variable.

A URI template is a URI-like string, containing one or more variable names. When you substitute values for these variables, the template becomes a URI. For example, the URI template http://www.example.com/users/{userId} contains the variable userId. Assigning the value fred to the variable yields http://www.example.com/users/fred.

It has the following optional elements:

  • name – name of the path variable to bind to
  • required – tells whether the path variable is required
  • value – alias for name

Watch the video

@PathVariable example

Let’s look at the development steps for @PathVariable annotation example –

Create spring boot project

There are different ways to create Spring boot project, you can refer the below articles to create spring boot project.

>> Create spring boot application using Spring initializer
>> Create spring boot application in Spring tool suite [STS]
>> Create spring boot application in IntelliJ IDEA

Update maven dependencies

Add the following maven dependencies to pom.xml and update the project

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
		<relativePath/> <!-- lookup parent from repository -->
	<description>Spring boot path variable</description>




spring-boot-starter-web dependency for building web applications using Spring MVC. It uses the tomcat as the default embedded container.

spring-boot-devtools dependency for automatic reload or live reload of application.

Create controller

Create UserController.java inside the in.bushansirgur.pathvariable.controller package and add the following content

package in.bushansirgur.pathvariable.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

public class UserController {
	public String printUserName(@PathVariable("userName") String name) {
		return "My name is "+name;
	public String printUserInfo(@PathVariable String name, 
								@PathVariable Long age,
								@PathVariable String location) {
		return "My name is "+name+". I am "+age+" years old. I am living in "+location;

Run the application

mvn spring-boot:run

Here the URI template, http://localhost:8080/users/{userName} contains variable userName. Now if the user makes a request http://localhost:8080/users/bushan the value bushan assigns to the variable userName in the URI template.

Inside the method argument we will use @PathVariable("userName") annotation to bind the value to the variable name in the method argument


the URI template, http://localhost:8080/users/{name}/{age}/{location} contains 3 variables name, age and location. Now if the user makes request http://localhost:8080/users/bushan/28/india then the values bushan, 28 and india assigns to the variable name, age and location respectively in the URI template.

Inside the method argument we will use 3 @PathVariable annotation to bind the values to the variable names in the method argument.


NOTE: If URI template variable name and method argument variable name both are same then we don’t need to specify variable names explicitly inside the @PathVariable annotation.

About the author

Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *