Spring Boot Optional Path Variables

The Spring Boot Optional Path Variable is used to handle the request urls that do not have path variable value. Optional Path variables in spring boot can be configured using the annotation @PathVariable. if the path variable is not configured as optional and the value of the path variable is not available in the request url, an error will be thrown.

We’ll see how to make the @PathVariable optional for the spring boot application in this post. If the path variable is configured in the method parameters, the value of the path variable is required in the request url. If the path variable’s value isn’t available, an exception would be thrown.

Spring boot uses the annotation @PathVariable to bind the template variable in the request url to the method parameter variable in the rest controller. The @PathVariable annotation in spring boot looks for the template key in the request url using the template and assigns it to the method variable. In certain instances, the path variable might not be available. The path variable should be considered optional in this situation.



Simple @PathVariable Example

The @PathVariable annotation identifies the value in the request url and assigns it to the method parameter value. The following example retrieves the template name value and assigns it 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 *