Spring Boot @PathVariable tutorial

Spring Boot @PathVariable tutorial shows how to read an URL template variable with @PathVariable annotation. We create a Spring Boot RESTful application to demonstrate the annotation.

Spring is a popular Java application framework and Spring Boot is an evolution of Spring which helps create stand-alone, production-grade Spring based applications easily.


@PathVariable is a Spring annotation which indicates that a method parameter should be bound to a URI template variable.

It has the following optional elements:

Spring Boot @PathVariable example

The following example creates a Spring Boot web application which uses @PathVariable. The application receives an URL from which it builds a text response to the client.

$ tree
├── pom.xml
└── src
    └── main
        └── java
            └── com
                └── zetcode
                    ├── Application.java
                    └── controller
                        └── MyController.java

This is the project structure of the Spring Boot application.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 

This is the Maven build file. The spring-boot-starter-web is a starter for building web applications using Spring MVC. It uses Tomcat as the default embedded container. The spring-boot-devtools is an artifact useful when developing Spring Boot applications; it allows automatic restart or live reload of applications. The application is packaged into a JAR file.

package com.zetcode.controller;

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

public class MyController {

    public String getMessage(@PathVariable("name") String name, 
            @PathVariable("age") String age) {
        String msg = String.format("%s is %s years old", name, age);
        return msg;

The controller processes the request from the client. It reads two values from the URL of the request.

public class MyController {

We have a RESTful web application.

public String getMessage(@PathVariable("name") String name, 
        @PathVariable("age") String age) {

With the @PathVariable annotation, we bind the request URL template path variable to the method variable. For instance, with the /July/28/ URL, the July value is bind to the name variable, and 28 value to the age variable.

String msg = String.format("%s is %s years old", name, age);

return msg;

We build the message and return it.

$ mvn spring-boot:run

We start the Spring Boot application.

$ curl localhost:8080/Robert/39/
Robert is 39 years old

We create a request to the application with the curl tool. The application responds with a message; the values were extracted from the URL with @PathVariable.

In this tutorial, we have created a RESTful web application with Spring Boot framework. We have demonstrated the usage of @PathVariable. You might also be interested in the related tutorials: Spring Boot @RequestParam tutorial, Spring Boot @ResponseBody tutorial, Spring Boot REST H2 tutorial, Standalone Spring applications, Java tutorial.