38

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

Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66
Anuj Kumar
  • 514
  • 1
  • 4
  • 8
  • They're functionally the same: they let you bind the value of a named HTTP param to the annotated variable. That being said, the question is very broad, so you'll have to specify more detail if you want a more useful answer. – nbrooks Nov 03 '14 at 07:41
  • 1
    my question is regarding their use in spring mvc controller... So please describe briefly... – Anuj Kumar Nov 03 '14 at 08:00

4 Answers4

52

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

nbrooks
  • 18,126
  • 5
  • 54
  • 66
Sergii Getman
  • 3,845
  • 5
  • 34
  • 50
  • 1
    Please also specify which(out of both) one i have to use when in spring mvc model controller – Anuj Kumar Nov 03 '14 at 08:00
  • 2
    QueryParam is from another framework and you are mentioning Spring. @Flao wrote that `@RequestParam` is from Spring and that should be used in Spring MVC. – ihsan Nov 03 '14 at 08:18
  • 1
    but i saw many Spring MVC Controller Example in which we use both(@QueryParam and @RequestParam annotation) type of annotation. That is why my question is that which one should be used when... – Anuj Kumar Nov 03 '14 at 08:23
  • Could you please provide links to them? – Sergii Getman Nov 03 '14 at 08:39
36

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

Jaydp
  • 1,029
  • 9
  • 17
Divya
  • 476
  • 4
  • 3
  • 1
    Also that `@RequestParam` is Spring Framework specific and needs no extra dependencies, except web related of course. Whereas, `@QueryParam` is under RESTful API for Web Service, hence has dependency of jsr311-api. – Imam Bux Mar 21 '19 at 12:29
20

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)

@RequestParam

@RequestParam on the other hand, belongs to Spring Framework, which states, that:

@RequestParam annotation indicates that a method parameter should be bound to a web request parameter.
• In Spring MVC, "request parameters" map to query parameters, form data, and parts in multipart requests. This is because the Servlet API combines query parameters and form data into a single map called "parameters", and that includes automatic parsing of the request body.


Therefore, I think the names may be indeed a little bit misleading, but after digging in a little bit more, they are quite self-explanatory. Just try to stick with one when you want only query parameter and with another when you want any request parameter.
Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66
2

In addition to above another difference I observed is QueryParam do not translate parameters values to List type however RequestParam does.

What I mean is "@RequestParam("para") List param" is able to convert comma separated param values to List, @QueryParam fails to translate this.

sarang
  • 104
  • 6