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 @PathVariable annotation in the spring boot finds the value of the template in the requested url. The request url can contain one or more path variables that are enclosed with curly braces. The @PathVariable determines the value and assigns to the method parameter variables.

In this post, we’ll explain what the @PathVariable Annotation is and how to use the @PathVariable Annotation with different options in the MVC Spring Boot framework.



Simple @PathVariable Request

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 Mapping with name

If the template name in the request url is different from the method parameter variable name, the annotation @PathVariable could not assign the value to the method parameter variable. In this case, the template name should be specified in the @PathVariable annotation to identify the value from the request url.

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/{username}", method=RequestMethod.GET)
	public String welcomepage(@PathVariable("username") String name) {
		return "Hello "+name;
	}
}

Output

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


Multiple @PathVariable Mapping in a Request

The request url may have one or more template variables that map multiple path variables in the method parameters. Each method parameter variable should be annotated with @PathVariable.

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/{username}/{password}", method=RequestMethod.GET)
	public String welcomepage(@PathVariable("username") String username, @PathVariable("password") String password) {
		return "Hello "+username+", Your password is "+password;
	}
}

Output

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


Multiple @PathVariable Mapping with HashMap

If the request url contains more than one template variable, all path variables should be added as method parameters. Instead, the hash map is annotated with @PathVariable, which retrieves all the values of the path variable from the request url.

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/{username}/{password}", method=RequestMethod.GET)
	public String welcomepage(@PathVariable HashMap<String, String> map) {
		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


Optional @PathVariable

The path variable can be set as an optional variable. Both combination request url should be added to the @RequestMapping 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/","/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 with Default value

The @PathVariable annotation does not support setting the default value. The request url is created with optional and then the null check is being used to assign a default value.

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) {
		if(name==null) {
			name = "Yawin Tutor";
		}
		return "Hello "+name;
	}
}

Output

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

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



Leave a Reply