JAX-RS queryparam example retrieving query parameter value from query string in Jersey rest API. In JAX-RS, getting values from queryString in jersey is very easy and it’s can be multiple ways. I’m trying to demonstrate with different approach. @QueryParam annotation is used to bind the queryString parameter in Jersery and it’s very handy.
Note: here orderBy parameter provided more than once, it means , sorting can be on multiple parameter and when you provide same key multiple times , jersey implementation convert int into list smartly.
1. Maven dependency for JAX-RS queryparam
You can choose version of jersey as per your choice.
com.sun.jersey jersey-server 1.9
2. Get Value from @QueryPram
package com.mysoftkey.jaxrs;
import java.util.List;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
/**
* This Service class is used to demonstrate to get values from queryString with different approach.
*
* @author Ranjeet Jha
*
*/
@Path("/queryParam")
public class JerseryQueryParamService {
/**
* In this method, retrieve few parameters from queryString in JAX-RS queryparam.
*
* service endpoint: http://localhost:8080/jersey/queryParam/withQuery?offset=1&len=10&orderBy=firstName&orderBy=lastName
* @param from
* @param to
* @param orderBy
* @return
*/
@GET
@Path("/withQuery")
public Response getParamsFromQueryString(@QueryParam("offset") int from,
@QueryParam("len") int to, @QueryParam("orderBy") List orderBy) {
return Response.status(200).entity(" offset : " + from + ", len : " + to
+ ", orderBy" + orderBy.toString()).build();
}
}
Service URL : http://localhost:8080/jersey/queryParam/withQuery?offset=1&len=10&orderBy=firstName&orderBy=lastName
output:
offset : 1, len : 10, orderBy[firstName, lastName]
3. Get values Programatically using @Context
@Context annotation used to bind request related param not only queryString. You can retrieve another parameter from HttpRequest.
/**
* In this method, retrieve multiple parameters from {@link UriInfo} for queryString.
*
* Service endpoint: http://localhost:8080/jersey/queryParam/withUriInfo?offset=1&len=10&orderBy=firstName&orderBy=lastName
*
* @param info
* @return
*/
@GET
@Path("/withUriInfo")
public Response getPersonInfo(@Context UriInfo info) {
String offset = info.getQueryParameters().getFirst("offset");
String len = info.getQueryParameters().getFirst("len");
List orderBy = info.getQueryParameters().get("orderBy");
return Response.status(200).entity("offset: " + offset + ", length : " + len + ", orderBy"
+ orderBy.toString()).build();
}
Service URL : http://localhost:8080/jersey/queryParam/withUriInfo?offset=1&len=10&orderBy=firstName&orderBy=lastName
Output:
offset: 1, length : 10, orderBy[firstName, lastName]
4. Get default Values using @DefaulatValue
@DefaultValue annotation used to bind the default value if parameter is not available in the request and if available in the request then bind provided value in that parameter. Here, I have not provided offset in the queryString so it takes the default value as 1 which is configured in the method annotation.
/**
* In this method, retrieve multiple parameters from JAX-RS queryParam with a default value,
* it means if the parameter is not available in queryString it takes the default value assigned to.
* in the following URL , offset we are not providing then it picks the default value i.e. 1.
*
* Service URL: http://localhost:8080/jersey/queryParam/withDefaultValue?len=10&orderBy=firstName&orderBy=lastName
*
* @param offset
* @param length
* @param orderBy
* @return
*/
@GET
@Path("/withDefaultValue")
public Response getQueryParamWithDefaultVale(
@DefaultValue("1") @QueryParam("offset") int offset,
@DefaultValue("10") @QueryParam("len") int length,
@DefaultValue("name") @QueryParam("orderBy") List orderBy) {
return Response.status(200).entity("offset : " + offset + ", length : " + length
+ ", orderBy" + orderBy.toString()).build();
}
Service URL:
http://localhost:8080/jersey/queryParam/withDefaultValue?len=10&orderBy=firstName&orderBy=lastName
Output:
offset : 1, length : 10, orderBy[firstName, lastName]
5. Download Source Code
Download source code of JAX-RS: Jersey CookieParam Example
6. References
Happy learning in the JAX-RS tutorial series for retrieving the query parameter from the query string.