Spring boot @PathVariable and @RequestParam annotations

Spring boot @PathVariable and @RequestParam annotations

We know that we can easily design APIs in a spring boot application. @PathVariable and @RequestParam are the most used annotations of the spring boot framework. Both Spring PathVariable and Spring RequestParam used to extract parameters from GET requests.

Sometimes we need to pass some values in our URL for processing the business logic. And to extract these values we use @PathVariable and @RequestParam annotation of the spring boot framework.

There are two types of URLs that you have seen when you call any GET request API.

1- http://localhost:8080/getUserByName/vasu

2- http://localhost:8080/getUserByName?name=vasu

See the difference between above two URLs. For example we have to fetch a user record by his name and we have to pass the name of user in the URL. So there are two ways you can pass this name in URL like above.

The difference in above two URL is.

1- In the first URL, we pass the name directly like /vasu this way of passing parameter fetched by the @PathVariable annotation.

2- In the Second URL, we pass a name with a parameter like ?name=vasu this way of passing a parameter fetched by the @RequestParam annotation.

Spring PathVariable and Spring RequestParam both annotations are present in Spring boot you just have to add the starter web dependency in your pom.xml file.

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Now create a Controller class and make two endpoints one for @PathVariable and another for @RequestParam.

package com.vasu.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

	@GetMapping("/getUserByName/{name}")
	public String getName(@PathVariable String name) {
		return "Your name using @Pathvariable is " + name;
	}

	@GetMapping("getUserByName")
	public String getNameReqParam(@RequestParam String name) {
		return "Your name using @RequestParam is " + name;
	}
}

Now run you application and check both the endpoints.

Output of @PathVariable endpoint is

spring pathvariable annotation

Output of @RequestParam endpoint is

spring requestparam annotaion

Everything is fine now. But there is a catch just focus on the below points.

1- Make sure the value inside your {} braces and the value you use with @PathVariable must be same otherwise you will get an exception.

For example @GetMapping(“/getUserByName/{name}“) and @PathVariable String name. In this we use same parameter that is name.

Just change the value and run the application for example if your @PathVariable endpoint is like below.

	@GetMapping("/getUserByName/{name1}")
	public String getName(@PathVariable String name) {
		return "Your name using @Pathvariable is " + name;
	}

In the above code, we change the parameter to {name1} and the parameter with @PathVariable is the name. Now run the application and hit the URL you will get an exception like below.

Spring pathvariable

And in console exception like below

[org.springframework.web.bind.MissingPathVariableException: Missing URI template variable 'name' for method parameter of type String]

So if you want to use the different parameter names in both places then you have to use the name attribute of @PathVariable annotation like below.

@GetMapping("/getUserByName/{name1}")
public String getName(@PathVariable(name = "name1") String name) {
return "Your name using @Pathvariable is " + name;
	}

Now run your application the same api will work now.

Do the same change and play with @RequestParam endpoint also and see the differences.

You may also like :

Spring boot security using JWT ( JSON WEB TOKENS ).

spring batch example

Spring boot cache example

Actuator in spring boot for monitoring application

Profiles in Spring boot application

Help Others, Please Share

Leave a Reply

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

x