Spring Web MVC @PathVariable

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 at least 8, Gradle 6.5.1, Maven 3.6.3, Spring Boot 2.3.3, 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:

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">





					<source>at least 8</source>
					<target>at least 8</target>

Spring Controller Class

I am going to create below Spring controller class that will handle incoming request and outgoing response.

package com.roytuts.spring.mvc.path.variable.controller;

import java.time.LocalDateTime;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

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 it in the 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>

Main Class

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

package com.roytuts.spring.mvc.path.variable;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SpringMvcPathVariableApp {

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


Testing the Application

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

spring mvc path variable

Source Code


Thanks for reading.

Leave a Reply

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