0% found this document useful (0 votes)
4 views

Spring @PathVariable and @RequestParam (+ Examples)

Description of @pathVariable And @RequestParam , And Their all property

Uploaded by

er.yadavbtech360
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Spring @PathVariable and @RequestParam (+ Examples)

Description of @pathVariable And @RequestParam , And Their all property

Uploaded by

er.yadavbtech360
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

12/15/24, 9:35 PM Spring @PathVariable and @RequestParam (+ Examples)

Spring @PathVariable and


@RequestParam
Learn about Spring @PathVariable and @RequestParam annotations and compare them
with Jakarta’s @PathParam and @QueryParam annotations.

Lokesh Gupta

July 25, 2023

Spring MVC
Spring Annotations, Spring WebMVC

This article explores the difference between @PathVariable and @RequestParam annotations
in Spring, as well as compares these to their equivalents in the Java/Jakarta EE provided
@PathParam and @QueryParam annotations. Additionally, we will look into the encoding of
parameter values and how to make the parameters optional to enhance flexibility.

1. Passing Parameters in URLs

Generally, a URL can contain two types of request parameters i.e. path parameters and
query parameters. Here’s an example of a URL that includes both:

http://localhost:8080/users/{id}/accounts?type=current&status=active

In the above URL:

id is the path parameter


type and status are the query parameters

https://howtodoinjava.com/spring-mvc/spring-pathvariable-and-requestparam/#:~:text=The %40PathVariable annotation is used,RESTful web se… 1/12


12/15/24, 9:35 PM Spring @PathVariable and @RequestParam (+ Examples)

2. Spring @PathVariable and @RequestParam


Annotations

Spring supports accepting path variables and query parameters using easy-to-use
annotations.

g("/users/{id}/accounts")
t<Accounts> getAccounts(@PathVariable String id, @RequestParam String type, @RequestPar

Consider the following request, and see how the request parts are populated into method
arguments of the handler method.

The @PathVariable extracts the URI template variables from the request URL. For
example, in the URI template “/users/{id}/accounts”, the value of id is mapped to a
value 101. In runtime, the method parameter id is initialized with the value 101 for this
reason.

The @RequestParam extracts the query parameters, form parameters, and even files
from the request. In our example, we have two query parameters: type and status. Any
value passed in these parameters will be used to initialize the method arguments type
and status.

This way, we can pass both dynamic values in the URL path and additional parameters as
query parameters to provide more information to the server when making a request.

https://howtodoinjava.com/spring-mvc/spring-pathvariable-and-requestparam/#:~:text=The %40PathVariable annotation is used,RESTful web se… 2/12


12/15/24, 9:35 PM Spring @PathVariable and @RequestParam (+ Examples)

If the same query parameter is passed multiple times in a URL then the first value
(lexically) of the parameter is used.

2.1. Naming Method Variables Different from URI Template


Variables

In the previous example, we didn’t define the name of the path variable since the names of
the method parameter and the path variable were the same. However, if the path variable
name is different from the URI template variable, we must provide the name in the
@PathVariable annotation:

In the following handler method, we are mapping the id path variable to userId method
parameter.

@GetMapping("/user/{id}")
public String getUserById(@PathVariable(name = "id") String userId) {
//...
}

Similarly, @RequestParam name also can be configured using the name attribute. In the
following example, any query parameter sent with ‘?name=‘ will be populated into username
variable.

@GetMapping("/users")
public String getUsers(@RequestParam(name = "name") String username) {
//...
}

2.2. Collecting Multiple Parameters and Values in a Map

Consider the URL: http://localhost:8080/users/{id}/{name}/accounts?


type=current&status=active. Similarly, there can be N number of path variables and query
parameters for a complex API. Additionally, the client may not be sending all the parameters
in each request, thus many variables will not be utilized in each request.

https://howtodoinjava.com/spring-mvc/spring-pathvariable-and-requestparam/#:~:text=The %40PathVariable annotation is used,RESTful web se… 3/12


12/15/24, 9:35 PM Spring @PathVariable and @RequestParam (+ Examples)

In such cases, when API has more than one path variable and request parameters, we can
use a Map to collect the parameters and values instead of naming each parameter
individually.

//Example URL: /users/location/IN/region/DEL?status=active&minAge=18&maxAge=60

@GetMapping("/users/country/{countryCode}/state/{stateCode}")
public String getUsers(@PathVariable Map<String, String> locationMap, @RequestParam

//...
}

In the above example, if we invoke the URL: /users/location/IN/region/DEL?


status=active&minAge=18&maxAge=60, the content of the maps will be:

locationMap => [countryCode=IN, stateCode=DEL]


queryMap => [status=active, minAge=18, maxAge=60]

3. Support for Optional Parameters

3.1. Using Attribute ‘require= false‘

By default, all the method parameters annotated with @RequestParam and


@PathVariable are required. To make a @PathVariable or @RequestParam parameter
optional, the required attribute can be used by setting its value to false. Its default value is
true.

In the following example, passing both query parameters, type and status, is optional.

https://howtodoinjava.com/spring-mvc/spring-pathvariable-and-requestparam/#:~:text=The %40PathVariable annotation is used,RESTful web se… 4/12


12/15/24, 9:35 PM Spring @PathVariable and @RequestParam (+ Examples)

@GetMapping("/user/{id}")
public String getAccounts(@PathVariable String id,
@RequestParam(required = false) String type,
@RequestParam(required = false) String status) {

//...
}

3.2. Using Java 8 Optional

Alternatively, we can use java.util.Optional to handle a non-mandatory path variable or


request params. TheOptional gives us the flexibility to either throw an error or provide a
default value if the value is missing.

The above example using Optional is shown below:

@GetMapping("/user/{id}")
public String getAccounts(@PathVariable Optional<String> String id,
@RequestParam Optional<String> type,
@RequestParam Optional<String> status) {

String userId = id. orElseThrow(...);


String userType = type.orElseGet(() -> "GUEST");
//...
}

4. Setting Default Values

Spring framework lacks a built-in capability to handle default values for method parameters
annotated with @PathVariable. Nevertheless, the use of java.util.Optional enables us to
ascertain whether the path variable holds a value or not. In case it turns out to be an empty
value, we have the option to handle the situation by providing a default value, as shown in the
previous section.

However, for method parameters annotated with @RequestParam we can use


the defaultValue attribute as a fallback when the parameter is not provided or has an empty
value. Using defaultValue attribute automatically makes a parameter optional (required =
false).

https://howtodoinjava.com/spring-mvc/spring-pathvariable-and-requestparam/#:~:text=The %40PathVariable annotation is used,RESTful web se… 5/12


12/15/24, 9:35 PM Spring @PathVariable and @RequestParam (+ Examples)

Consider a URL: http://localhost:8080/users?pageNo=1&pageSize=20. We can set a


default value for pageNo and pageSize parameters as shown below:

@GetMapping("/users")
public String getAllUsers(@RequestParam(value = "pageNo", defaultValue = "0") int pa
@RequestParam(value = "pageSize", defaultValue = "30") int pageSize) {

//...
}

5. Passing Encoded vs Exact Values to Parameters


The value of @PathVariable parameter is extracted from the URI path so it cannot be not
encoded. HTTP request paths cannot have encoded values.

For example, Consider URL: http://localhost:8080/user/A%20BC. The controller method


and response returned will be:

@GetMapping("/user/{id}")
public String getUserById(@PathVariable String Id) {
return "User Id: " + id;
}

---- response ----


User Id: A%20BC

The value is mapped to id parameter as it is without being decoded.

The value of @RequestParam parameters are automatically encoded in the URL when sent,
and the controller subsequently decodes it upon receiving.

For example, the controller method and response for URL: http://localhost:8080/users?
name=John%20Doe will be:

@GetMapping("/users")
public String getUsers(@RequestParam String name) {
return "Name: " + name;
}

https://howtodoinjava.com/spring-mvc/spring-pathvariable-and-requestparam/#:~:text=The %40PathVariable annotation is used,RESTful web se… 6/12


12/15/24, 9:35 PM Spring @PathVariable and @RequestParam (+ Examples)

---- response ----


Name: John Doe

Here the single space in the name “John Doe” is URL encoded to “%20” while sending the
request to the server.

6. FAQs

6.1. What is the difference between @PathVariable and


@PathParam?

Both, @PathParam and @PathVariable, annotations extract values from the URI path of a
request URL. However, they are associated with different frameworks.

The @PathVariable annotation is used in the Spring framework. Whereas, @PathParam


annotation is used in the Java/Jakarta EE (Enterprise Edition) framework, specifically in JAX-
RS, which is a specification for building RESTful web services in Java. It is available to use in
frameworks that implement the specification, such as RESTEasy.

Here’s an example of how we would use @PathParam on a JAX-RS resource method:

@GET
@Path("/user/{id}")
public Response getUserById(@PathParam("id") String id) {

// ...
}

6.2. What is the difference between @QueryParam and


@RequestParam?

Both, @QueryParam and @RequestParam, binds the value(s) of a HTTP query parameter to a
resource method parameter.

The @RequestParam annotation is part of the Spring framework. Whereas, @QueryParam


annotation is part of the JAX-RS specification and is available to use in frameworks that

https://howtodoinjava.com/spring-mvc/spring-pathvariable-and-requestparam/#:~:text=The %40PathVariable annotation is used,RESTful web se… 7/12


12/15/24, 9:35 PM Spring @PathVariable and @RequestParam (+ Examples)

implement the specification.

Here’s an example of how @QueryParam can be used in JAX-RS:

@GET
@Path("/users")
public Response getUsers(@QueryParam("name") String name) {
// Method implementation
}

7. Conclusion

In this Spring annotations tutorial, we learned the differences


between @RequestParam and @PathVariable in capturing the request parameters and query
parameters. We also learned about setting default values to parameters, making them
optional as well as passing encoded vs exact values.

We also learned about @PathParam and @QueryParam and their usage.

Happy Learning !!

Weekly Newsletter
Stay Up-to-Date with Our Weekly Updates. Right into Your Inbox.

Email Address

SUBSCRIBE

Comments

 Subscribe 

https://howtodoinjava.com/spring-mvc/spring-pathvariable-and-requestparam/#:~:text=The %40PathVariable annotation is used,RESTful web se… 8/12


12/15/24, 9:35 PM Spring @PathVariable and @RequestParam (+ Examples)

Be the First to Comment!

{} [+] 

0 COMMENTS

Spring MVC Tutorial

Getting Started

DispatcherServlet

WebMvcConfigurer
ProblemDetail and ErrorResponse

@RequestMapping

@PathVariable and @RequestParam

Custom Validator

JSR-303 Validation

Custom Validation Annotation

MessageSourceAware

I18n and i10n

Interceptor

HandlerInterceptor
File Download

Multi File Upload

Multi File Upload (Ajax)

XmlViewResolver

InternalResourceViewResolver
ResourceBundleViewResolver

SimpleMappingExceptionResolver

Handling Dropdown

Hibernate CRUD Example

Interview Questions

https://howtodoinjava.com/spring-mvc/spring-pathvariable-and-requestparam/#:~:text=The %40PathVariable annotation is used,RESTful web se… 9/12


12/15/24, 9:35 PM Spring @PathVariable and @RequestParam (+ Examples)

Lokesh Gupta

A fun-loving family man, passionate about computers and problem-solving, with over 15
years of experience in Java and related technologies. An avid Sci-Fi movie enthusiast and a
fan of Christopher Nolan and Quentin Tarantino.

Follow on Twitter
Portfolio

PREVIOUS

Spring Boot RestClient (with Examples)

NEXT

https://howtodoinjava.com/spring-mvc/spring-pathvariable-and-requestparam/#:~:text=The %40PathVariable annotation is used,RESTful web s… 10/12


12/15/24, 9:35 PM Spring @PathVariable and @RequestParam (+ Examples)

Java – Convert Decimal to Hexadecimal and Vice-


versa

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related


technologies.

It also shares the best practices, algorithms & solutions and frequently
asked interview questions.

Tutorial Series

OOP

Regex

Maven

Logging

TypeScript

Python

Meta Links

About Us

Advertise

Contact Us

Privacy Policy

Our Blogs

https://howtodoinjava.com/spring-mvc/spring-pathvariable-and-requestparam/#:~:text=The %40PathVariable annotation is used,RESTful web s… 11/12


12/15/24, 9:35 PM Spring @PathVariable and @RequestParam (+ Examples)

REST API Tutorial

Follow On:

Copyright © 2024 | Sitemap

Table Of Contents

1. Passing Parameters in URLs

2. Spring @PathVariable and @RequestParam Annotations


2.1. Naming Method Variables Different from URI Template Variables

2.2. Collecting Multiple Parameters and Values in a Map

3. Support for Optional Parameters


3.1. Using Attribute ‘require= false‘

3.2. Using Java 8 Optional


https://howtodoinjava.com/spring-mvc/spring-pathvariable-and-requestparam/#:~:text=The %40PathVariable annotation is used,RESTful web s… 12/12

You might also like