Spring Boot Web MVC @PathVariable

Path Variable

This tutorial will show you how to use @PathVariable annotation in Spring MVC. The @PathVariable annotation is used on a method argument to bind it to the value of a URI template variable. You can also make this path variable optional in the URI. Path variable can appear in any path segment of the URI.

Prerequisites

Java 8+ (19), Gradle 6.5.1, Maven 3.6.3 – 3.8.5, Spring Boot 2.3.3/3.0.5, Thymeleaf Template

Project Setup

You can create either gradle or maven based project in your favorite IDE or tool. The name of the project is spring-mvc-path-variable.

If you are creating gradle based project then use below build.gradle script:

buildscript {
	ext {
		springBootVersion = '2.3.3.RELEASE'
	}
	
    repositories {
    	mavenCentral()
    }
    
    dependencies {
    	classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

plugins {
    id 'java-library'
    id 'org.springframework.boot' version "${springBootVersion}"
}

sourceCompatibility = 12
targetCompatibility = 12

repositories {
    mavenCentral()
}

dependencies {
	implementation "org.springframework.boot:spring-boot-starter-web:${springBootVersion}"
	implementation("org.springframework.boot:spring-boot-starter-thymeleaf:${springBootVersion}")
}

If you are creating maven based project then use below pom.xml file:

<?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.roytuts</groupId>
	<artifactId>spring-mvc-path-variable</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>19</maven.compiler.source>
		<maven.compiler.target>19</maven.compiler.target>
	</properties>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.0.5</version>
	</parent>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

Spring REST Controller

I am going to create below Spring controller class that will handle incoming request and outgoing response for the clients or end users.

@Controller
public class HelloWorldController {

	@GetMapping("/hello/{msg}")
	public String helloWorld(@PathVariable String msg, Model model) {
		//model.addAttribute("today", LocalDateTime.now());
		model.addAttribute("msg", msg);

		return "hello";
	}

}

To process the @PathVariable annotation, Spring MVC needs to find the matching URI template variable by name. You can specify the name in the @PathVariable annotation:

@GetMapping("/hello/{msg}")
	public String helloWorld(@PathVariable("msg") String msg, Model model) {
    ...
}

Or if the URI template variable name matches the method argument name you can omit that detail. As long as your code is not compiled without debugging information, Spring MVC will match the method argument name to the URI template variable name:

@GetMapping("/hello/{msg}")
	public String helloWorld(@PathVariable String msg, Model model) {
    ...
}

View File – UI

The following view file displays the value of path variable’s value when rendered in the browser. This template file – hello.html – is kept under src/main/resources/templates folder.

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Spring MVC Path Variable</title>
</head>
<body>
	<p>
		 Path Variable's Value : <!-- <strong th:text="${today}"></strong>, --> <strong th:text="${msg}"></strong>
	</p>
</body>
</html>

Spring Boot Main Class

A class having main method with @SpringBootApplication is enough to deploy the application into embedded Tomcat server.

@SpringBootApplication
public class SpringMvcPathVariableApp {

	public static void main(String[] args) {
		SpringApplication.run(SpringMvcPathVariableApp.class, args);
	}

}

Testing Spring MVC Path Variable

Now hit the URL http://localhost:8080/hello/Good%20Afternoon in the browser, you will see the below output in the browser:

spring mvc path variable

Source Code

Download

Leave a Reply

Your email address will not be published. Required fields are marked *