Spring Boot @PathVariable - Read URI Template Variable with @PathVariable

In this short article, we will learn how to use Spring Boot @PathVariable annotation to read an URL template variable. We create a Spring Boot RESTful application to demonstrate the annotation.

@PathVariable Annotation

@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:
  • name - name of the path variable to bind to
  • required - tells whether the path variable is required
  • value - alias for name
With the @PathVariable annotation, we bind the request URL template path variable to the method variable. For instance, with the /100/Ramesh/ URL, the 100 value is bind to the id variable and "Ramesh" value to the name variable.
    @GetMapping(path = "/hello-world/{id}/{name}")
    public HelloWorldBean helloWorldPathVariable(@PathVariable long id, 
        @PathVariable(name = "name") String name) {
        return new HelloWorldBean(id, name);
    }

Spring Boot @PathVariable Annotation 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.

Development Steps

  1. Create a Spring Boot Application
  2. Project Structure
  3. Pom Dependencies
  4. Java Bean - HelloWorldBean.java
  5. Create REST Controller - HelloWorldController.java
  6. Run Application - Application.java
  7. Testing

1. Create a Spring Boot Application

There are many ways to create a Spring Boot application. You can refer below articles to create a Spring Boot application.
Refer project structure or packaging structure in the next step.

2. Project Structure

This is the project structure of the Spring Boot application that we are going to create -

3. Pom Dependencies

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.
<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/>
        <!-- lookup parent from repository -->
    </parent>
    <groupId>net.javaguides.springboot</groupId>
    <artifactId>springboot-helloworld-app</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-helloworld-app</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

4. Java Bean - HelloWorldBean.java

Let's create a representation class which we use to return in JSON format:
package net.javaguides.springboot;

public class HelloWorldBean {

    private long id;
    private String message;

    public HelloWorldBean(long id, String message) {
        super();
        this.id = id;
        this.message = message;
    }

    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
}

5. Create REST Controller - HelloWorldController.java

Let's create a simple HelloWorldController which exposes hello world rest service:
package net.javaguides.springboot;

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

@RestController
public class HelloWorldController {

    @GetMapping(path = "/hello-world/{id}/{name}")
    public HelloWorldBean helloWorldPathVariable(@PathVariable long id, 
        @PathVariable(name = "name") String name) {
        return new HelloWorldBean(id, name);
    }
}
With the @PathVariable annotation, we bind the request URL template path variable to the method variable. For instance, with the /100/Ramesh/ URL, the 100 value is bind to the id variable and "Ramesh" value to the name variable.
    @GetMapping(path = "/hello-world/{id}/{name}")
    public HelloWorldBean helloWorldPathVariable(@PathVariable long id, 
        @PathVariable(name = "name") String name) {
        return new HelloWorldBean(id, name);
    }

6. Run Application - Application.java

Application is the entry point which sets up the Spring Boot application. The @SpringBootApplication annotation enables auto-configuration and component scanning.
Let's run this Spring boot application from either Eclipse IDE by right click -> Run As -> Java Application.
Or you can use below maven command to run:
mvn spring-boot:run

7. Testing from Browser

Hit this URL in a browser - http://localhost:8080/hello-world/100/ramesh
Get source code of this tutorial on my GitHub Repository.

Comments