Member-only story

@RequestParam vs @QueryParam vs @PathVariable vs @PathParam

Mahdi Razavi
1 min readJun 22, 2019

--

The annotations @RequestParam, @QueryParam and @PathVariable, PathParam are used to read values ​​from the request. But which one is used for what?
The arrangement in the collection is deliberately grouped, as these are annotations that have the same task but come from different frameworks that often occur in combination.

comparison

As shown in the table, the difference lies in where a value is read out. @PathParam reads the value from a path part of the called URI. @QueryParam is used to read the values ​​from QueryParameters of a URI call. These are after? listed in a URI.
PathParams are location-dependent, while QueryParams are passed as a key value pair and therefore their order is irrelevant to more than one QueryParam.

example
As an example again both calls in a URI:

http://www.xyz.ir/ <@PathParam> /? queryParamName = <@QueryParm>

@PathVariable

This annotation is used on the method parameter we want to populate:

@RequestMapping(value = "/orders/{id}", method = RequestMethod.GET)
@ResponseBody
public String getOrder(@PathVariable final String id) {
return "Order ID: " + id;
}

Even though @PathVariable and @RequestParam are both used to extract values from the URI, their usage is largely determined by how a site is designed.

The @PathVariable annotation is used for data passed in the URI (e.g. RESTful web services) while @RequestParam is used to extract the data found in query parameters.

Refrence
http://www.nullpointer.at/2017/11/22/requestparam-queryparam-pathvariable-pathparam/
Related Links
https://docs.oracle.com/javaee/7/api/javax/ws/rs/PathParam.html
https://docs.oracle.com/javaee/7/api/javax/ws/rs/QueryParam.html
https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/PathVariable.html
https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestParam.html

--

--

Mahdi Razavi
Mahdi Razavi

Written by Mahdi Razavi

A software engineer who enjoys technical challenges.

Responses (1)

Write a response