Spring boot @PathVariable annotation Example





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

Overview


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">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.2</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>in.bushansirgur</groupId>
	<artifactId>pathvariable</artifactId>
	<version>v1</version>
	<packaging>war</packaging>
	<name>pathvariable</name>
	<description>Spring boot path variable</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-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</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>

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;

@RestController
public class UserController {
	
	@GetMapping("/users/{userName}")
	public String printUserName(@PathVariable("userName") String name) {
		return "My name is "+name;
	}
	
	@GetMapping("/users/{name}/{age}/{location}")
	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

01

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.

02

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.



Our Best Partners: https://crazytime-bangladesh.com/bn/ fortune mouse jogar https://kasinopaynplay.com/ lucky. jet. gambling. game. besten casino mit google pay bezahlen https://kasinopaynplay.com/ wheels casino casino dansk suomalaiset pikakasinot mines.

Bushan Sirgur

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

Leave a Reply