In RESTful web services (JAX-RS) @QueryParam annotation will be used to get the query parameters from the URL, Observe carefully, i am saying we will retrieve the parameters only not their values. But in case of @PathParam we will get parameter values directly.

In JAX-RS, you can use @QueryParam annotation to inject URI query parameter into Java method. for example,

/users/query?url=codenuclear.com

In above URI pattern, query parameter is “url=codenuclear.com“, and you can get the url value with @QueryParam(“url”).

Here is the final pom.xml file having required dependencies.

Now add Jersey servlet to our deployment descriptor web.xml as front controller.

  1. @QueryParam example

    See a full example of using @QueryParam in JAX-RS.

  2. Output

    URI pattern : “http://localhost:8080/WebServices_JAX_RS/rest/users/query?from=100&to=200&orderBy=age&orderBy=name

  3. Programmatic Query Parameter

    Alternatively, you can get the query parameters grammatically, via “@Context UriInfo“. See equivalent version below :

  4. Output

    URI pattern : “http://localhost:8080/WebServices_JAX_RS/rest/users/query?from=300&to=500&orderBy=age&orderBy=name

  5. @DefaultValue example

    @DefaultValue is good for optional parameter.

  6. Output

    URI pattern : “http://localhost:8080/WebServices_JAX_RS/rest/users/query

It's good to share...Share on FacebookTweet about this on TwitterShare on LinkedInPin on PinterestShare on Google+Email this to someone

2 Thoughts on “JAX-RS @QueryParam Example in Java”

Leave a Reply

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