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

@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.

pom.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>4.0.0</modelVersion>
    <groupId>com.zetcode</groupId>
    <artifactId>SpringBootPathVariable</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
    </parent>      
    
    <dependencies>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>     
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>         
        
    </dependencies>
</project>

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.

MyController.java
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;

@RestController
public class MyController {

    @RequestMapping(path="/{name}/{age}")
    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.

@RestController
public class MyController {

We have a RESTful web application.

@RequestMapping(path="/{name}/{age}")
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.