JAX-RS @QueryParam with Example – RESTful Web Services Tutorial

3 months ago codedestine 0

In JAX-RS @QueryParam annotation is used to bind query parameter value defined in the @Path ( URL ) expression to the method arguments. 

Let’s try to understand it with an example.

Example 1 :-

In this example of JAX-RS @QueryParam, we will send two query parameters ( from and to ) in the @Path expression books/query. If we hit this URL <base URL>/books/query?from=10&to=100, The value 10 and 100 will get copy into @QueryParam(“from”) int fromBook and @QueryParam(“to”) int toBook variable.

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("books")
public class QueryParamExample {

   @GET
   @Produces(MediaType.TEXT_PLAIN)
   @Path("/query")
   public Response getListOfBooks(@QueryParam("from") int fromBook,
  		@QueryParam("to") int toBook){
	return Response.status(200)
  		       .entity("Books from " + fromBook + " toBook " + toBook).build();
   }
	
}

Result :-

JAX-RS @QueryParam

Example 2 :-

In this example, we will send one parameter ( orderBy ) two times in the @Path expression books/query. If we hit this URL <base URL>/books/query?orderBy=books&orderBy=chapters, The values will get copy into @QueryParam(“orderBy”) String orderBy variable as a List.

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("books")
public class QueryParamExample {

   @GET
   @Produces(MediaType.TEXT_PLAIN)
   @Path("/query")
   public Response getListOfBooksOrderBy(@QueryParam("orderBy") List<String> orderBy){
  	String order = orderBy.stream().reduce( (x,y) -> x + " , " + y ).get();
	return Response.status(200)
  		       .entity("List of Books order by : " + order).build();
   }
	
}

Result :-

JAX-RS @QueryParam

Example 3 :-

In this example, we will send two query parameters ( from and to ) in the @Path expression books/query. Here, @DefaultValue annotation is used, so that if values of the query parameter are not sent in the URL. Then default values are bind with the method arguments. 

import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("books")
public class QueryParamExample {

   @GET
   @Produces(MediaType.TEXT_PLAIN)
   @Path("/query")
   public Response getListOfBooksDefaultValue(@DefaultValue("0") @QueryParam("from") int fromBook,
  		@DefaultValue("10") @QueryParam("to") int toBook){
	return Response.status(200)
   		       .entity("Books from " + fromBook + " toBook " + toBook).build();
   }
	
}

Result :-

JAX-RS @QueryParam

Example 4 :-

In this example, we will use @Context annotation to get the values of the passed query parameters.

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

@Path("books")
public class QueryParamExample {

   @GET
   @Produces(MediaType.TEXT_PLAIN)
   @Path("/query")
   public Response getListOfBooks(@Context UriInfo urlInfo){
	String fromBook = urlInfo.getQueryParameters().getFirst("from");
	String toBook = urlInfo.getQueryParameters().getFirst("to");
	List<String> orderBy = urlInfo.getQueryParameters().get("orderBy");
	String order = orderBy.stream().reduce( (x,y) -> x + " , " + y ).get();
	return Response.status(200)
		.entity("Books from " + fromBook + " toBook " + toBook + " order " + order).build();
   }
	
}

Result :-

JAX-RS @QueryParam

References :-

  1. @QueryParam Java Docs
  2. @DefaultValue Java Docs
  3. @Context Java Docs
  4. @UriInfo Java Docs

That’s all for JAX-RS @QueryParam with Example. If you liked it, please share your thoughts in comments section and share it with others too.