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.


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 {
    dependencies {

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

sourceCompatibility = 12
targetCompatibility = 12

repositories {

dependencies {
	implementation "org.springframework.boot:spring-boot-starter-web:${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">

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.

public class HelloWorldController {

	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:

	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:

	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">
<title>Spring MVC Path Variable</title>
		 Path Variable's Value : <!-- <strong th:text="${today}"></strong>, --> <strong th:text="${msg}"></strong>

Spring Boot Main Class

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

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


Leave a Reply

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