Spring REST Optional Path Variable Example

Optional Path Variable

In this tutorial I will show you how to work with Spring framework’s optional path variable (PathVariable) in REST services. Here I will use Java 8’s java.util.Optional or using attribute required=false and at least Spring Boot 2.1.4 to support optional PathVariable. The optional variable can be found in the URI resource path of the URL.


Java 8/19, Spring Boot 2.1.4/3.1.0, Gradle 4.10.2, Maven 3.8.5

Project Setup

Create a gradle based or maven based project in your favorite IDE or tool.

If you are creating maven based project then you can use the following pom.xml file for your project:

<?xml version="1.0" encoding="UTF-8"?>

<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">




Here in the below build.gradle file I have added the required dependencies such as Java, Spring Boot.

buildscript {
	ext {
		springBootVersion = '2.1.4.RELEASE'
    repositories {
    dependencies {
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
dependencies {

Spring REST Controller

Create below REST Controller class that will handle end-user’s request and response.

In the below method getHi() I have passed an argument with attribute required=false to make the parameter optional.

In the below method getHello() I have passed an argument with Java 8’s new feature java.util.Optional to make the parameter optional.

Notice there are two endpoints for GetMapping because when you do not pass any value for PathVaribale ‘s name then simply putting URI /hi or /hello in http request will not work and you will get 404 – Resource Not Found in the browser.

So in order to let http request know that when you do not pass any value for PathVariable name then map URI to /hi instead of /hi/{name} and /hello instead of /hello/{name}.

Notice how I have passed spring optional path variable in the REST method for data type String.

public class SpringRestController {
	@GetMapping(value = { "/hi/{name}", "/hi" })
	public ResponseEntity<String> getHi(@PathVariable(required = false) String name) {
		return new ResponseEntity<String>(
				"Hi " + (name == null || name.length() == 0 ? "Anonymous" : name) + ", Good Morning!", HttpStatus.OK);

	@GetMapping(value = { "/hello/{name}", "/hello" })
	public ResponseEntity<String> getHello(@PathVariable Optional<String> name) {
		return new ResponseEntity<String>("Hello " + (name.isPresent() ? name.get() : "Anonymous") + ", Good Morning!",


Spring Boot Main Class

As you know Spring Boot application with @SpringBootApplication annotation and the main method takes care of deployment of the application into default embedded Tomcat server.

public class App {

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


Testing Optional Path Variable

Check outputs in browsers for below URLs for the example spring optional path variable.

When you do not pass name into the path variable:

spring rest optional path variable
spring rest optional path variable

When you pass the name into the path variable:

spring rest optional path variable
spring rest optional path variable

Source Code


Leave a Reply

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