Spring Boot Optional Path Variables

In this post, we’ll see how to make the @PathVariable optional for the spring boot application. The path variable value is needed in the url request if the path variable is configured in the method parameters. If the value of the path variable is not available, the exception would be thrown.

The annotation @PathVariable in the spring boot is used to bind the template variable in the request url to the method parameter variable in the rest controller. The spring boot @PathVariable annotation finds the template value in the request url using the template and attaches the value to the method variable. The path variable might not be available in certain situations. In this case, the path variable should be considered as optional.



Simple @PathVariable Example

The annotation @PathVariable finds the value in the request url and assigns to the method parameter value. The example below retrieves the value for the template {name} and assigns to the method parameter variable.

package com.yawintutor.application;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

	@RequestMapping(value="/welcome/{name}", method=RequestMethod.GET)
	public String welcomepage(@PathVariable String name) {
		return "Hello "+name;
	}
}

Output

$ curl -X GET http://localhost:8080/welcome/yawin
Hello yawin


@PathVariable Optional using required false

The path variable is configured in the @RequestMapping annotation. The annotation @RequestMapping allows you to configure multiple request urls. The request url with and without a path variable should be configured in the @RequestMapping annotation. 

If the request url is invoked, the rest controller will execute a method without the value of the path attribute. But the @PathVariable annotation is requiring a value to be configured. @PathVariable should be made as an optional using required=false.

package com.yawintutor.application;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

	@RequestMapping(value= {"/welcome/","/welcome/{name}"}, method=RequestMethod.GET)
	public String welcomepage(@PathVariable(value="name",required = false) String name) {
		return "Hello "+name;
	}
}

Output

$ curl -X GET http://localhost:8080/welcome/yawin
Hello yawin

$ curl -X GET http://localhost:8080/welcome/
Hello null


@PathVariable using Optional class

The path variable in the method parameter should be configured using the Optional class. If the value is not obtained in the @PathVariable annotation, the Optional class will be given with null value.

package com.yawintutor.application;

import java.util.Optional;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

	@RequestMapping(value= {"/welcome/","/welcome/{name}"}, method=RequestMethod.GET)
	public String welcomepage(@PathVariable(value="name") Optional<String> name) {
		if(name.isPresent()) {
			return "Hello "+name;
		} else {
			return "Hello Guest";
		}
	}
}

Output

$ curl -X GET http://localhost:8080/welcome/yawin
Hello yawin

$ curl -X GET http://localhost:8080/welcome/
Hello Guest


@PathVariable using Multiple Rest Controller Methods

In the rest controller class, two methods should be created for two request urls, one with a path value and the other without a path value. The two method will have two implementation based on the path value. The example below illustrates how to create two rest controller methods to manage an optional @PathVariable annotation.

package com.yawintutor.application;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

	@RequestMapping(value= {"/welcome/{name}"}, method=RequestMethod.GET)
	public String welcomepage1(@PathVariable(value="name") String name) {
		return "Hello "+name;
	}
	
	@RequestMapping(value= {"/welcome/"}, method=RequestMethod.GET)
	public String welcomepage2() {
		return "Hello Guest";
	}
}

Output

$ curl -X GET http://localhost:8080/welcome/yawin
Hello yawin

$ curl -X GET http://localhost:8080/welcome/
Hello Guest


@PathVariable Optional using Map

If the path variables are more than one, the combination url count is exponential. In this case, the hash map is used to read all path variables with or without values.

package com.yawintutor.application;

import java.util.HashMap;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

	@RequestMapping(value= {"/welcome/","/welcome/{username}","/welcome/{username}/{password}"}, method=RequestMethod.GET)
	public String welcomepage(@PathVariable HashMap<String, String> map) {
		if(map.get("username")==null) {
			return "Hello Guest, Your password is empty";
		}
		return "Hello "+map.get("username")+", Your password is "+map.get("password");
	}
}

Output

$ curl -X GET http://localhost:8080/welcome/yawin/tutor
Hello yawin, Your password is tutor

$ curl -X GET http://localhost:8080/welcome/
Hello Guest, Your password is empty



Related Articles

Leave a Reply

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