Spring @PathVariable Annotation Example
In this section we will learn about @PathVariable Annotation.
The @PathVariable annotation used on a method argument to bind it to the value of 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 /India/Knowledgefactory/ URL, the India value is bind to the country variable and "Knowledgefactory" value to the name variable.
@GetMapping(path = "/users/{country}/{name}")
public User getUserByCountryAndName(@PathVariable String country,
@PathVariable(name = "name") String name) {
...........
}
The following example creates a Spring Boot web application which uses @PathVariable. The application receives an URL from which it builds a json response to the client.
Project Directory
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
https://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>3.0.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.knf.dev.demo</groupId>
<artifactId>spring-pathvariable-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-pathvariable-example</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</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>
User.java
package com.knf.dev.demo.dto;
public class User {
private String name;
private String country;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public User(String name, String country) {
this.name = name;
this.country = country;
}
}
UserController.java
package com.knf.dev.demo.controller;
import com.knf.dev.demo.dto.User;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/v1")
public class UserController {
@GetMapping(value = "/users/{country}/{name}")
public User getUserByCountryAndName
(@PathVariable String country,
@PathVariable(name = "name") String name) {
return new User(name,country);
}
}
Run Application - Application.java
Application is the entry point that sets up the Spring Boot application. The @SpringBootApplication annotation enables auto-configuration and component scanning.
Let's run this Spring boot application from either IntelliJ IDEA IDE by right click - Run 'Application.main()'
Or you can use the below maven command to run:
mvn spring-boot:run
Try to send a GET request to the /users/{country}/{name}
endpoint using Postman.