What Is the Difference B/W @Requestparam and @Queryparam Anotation

What is main difference between @RequestParam and @QueryParam in Spring MVC controller?

2

4 Answers

@QueryParam is a JAX-RS framework annotation and @RequestParam is from Spring.

4

Apart from these mentioned differences in framework, one major difference is @RequestParam will always expect a value to bind. Hence, if value is not passed, it will give error. This is not the case in @QueryParam

To explicitly give the option, use required = false while using @RequestParam

1

I'll try to shed some more detailed light on this question.

Let's start with semantics of Query Parameter vs. Request Parameter, respectively @QueryParam vs. @RequestParam

Query Parameter, according to the HTTP Specification and also according to this annotation's (@QueryParam) implementation JAX-RS, represents the parameter found in query string. i.e. the string which follows the question-mark in the full request URL.

Request Parameter, according to the HTTP Specification and also according to this annotation's (@RequestParam) implementation Spring MVC (org.springframework.web.bind.annotation), represents the parameter of HTTP request, and to be clear here, it doesn't specify which type of parameter it is - query, header, body or etc.


Now let's talk about implementations per se.

@QueryParam

@QueryParam annotation belongs to JAX-RS specification, which states, that:

@QueryParam Binds the value(s) of a HTTP query parameter to a resource method parameter, resource class field, or resource class bean property

Pay attention here, it's query parameter which is bound to resource method's parameter (i.e. parameter in query string)

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.