Introduction
In this tutorial I will show you how to define Spring optional path variable (PathVariable) in REST service. 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.
Prerequisites
Java 8, Spring Boot 2.1.4, Gradle 4.10.2, Eclipse
Example with Source Code
Creating Project
Create a gradle based project in Eclipse IDE. The project structure would look like similar to the below image:

Updating Build Script
Here in the below build.gradle file we have added the required dependencies such as Java, Spring Boot.
buildscript {
ext {
springBootVersion = '2.1.4.RELEASE'
}
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web:${springBootVersion}")
}
Creating 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 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 we have passed spring optional path variable in the REST method for data type String.
package com.roytuts.spring.rest.optional.controller;
import java.util.Optional;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
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!",
HttpStatus.OK);
}
}
Creating Main Class
As we know Spring Boot application has the main class that takes care of deployment of the application into default embedded Tomcat server.
package com.roytuts.spring.rest.optional;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(scanBasePackages = "com.roytuts.spring.rest.optional")
public class SpringRestOptional {
public static void main(String[] args) {
SpringApplication.run(SpringRestOptional.class, args);
}
}
Testing the Application
Check outputs in browsers for below URLs for the example spring optional path variable.
When you do not pass name into the path variable:


When you pass the name into the path variable:


Source Code
Thanks for reading.