JAX-RS @QueryParam example

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


/users/query?url=mkyong.com

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

1. @QueryParam example

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


import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

@Path("/users")
public class UserService {

	@GET
	@Path("/query")
	public Response getUsers(
		@QueryParam("from") int from,
		@QueryParam("to") int to,
		@QueryParam("orderBy") List<String> orderBy) {

		return Response
		   .status(200)
		   .entity("getUsers is called, from : " + from + ", to : " + to
			+ ", orderBy" + orderBy.toString()).build();

	}

}

URI Pattern : “users/query?from=100&to=200&orderBy=age&orderBy=name


getUsers is called, from : 100, to : 200, orderBy[age, name]
Like it ?
@QueryParam will convert the query parameter “orderBy=age&orderBy=name” into java.util.List automatically.

2. Programmatic Query Parameter

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


import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

@Path("/users")
public class UserService {
	
	@GET
	@Path("/query")
	public Response getUsers(@Context UriInfo info) {

		String from = info.getQueryParameters().getFirst("from");
		String to = info.getQueryParameters().getFirst("to");
		List<String> orderBy = info.getQueryParameters().get("orderBy");
		
		return Response
		   .status(200)
		   .entity("getUsers is called, from : " + from + ", to : " + to
			+ ", orderBy" + orderBy.toString()).build();

	}

}

URI Pattern : “users/query?from=100&to=200&orderBy=age&orderBy=name


getUsers is called, from : 100, to : 200, orderBy[age, name]

3. @DefaultValue example

@DefaultValue is good for optional parameter.


import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

@Path("/users")
public class UserService {

	@GET
	@Path("/query")
	public Response getUsers(
		@DefaultValue("1000") @QueryParam("from") int from,
		@DefaultValue("999")@QueryParam("to") int to,
		@DefaultValue("name") @QueryParam("orderBy") List<String> orderBy) {

		return Response
		   .status(200)
		   .entity("getUsers is called, from : " + from + ", to : " + to
			+ ", orderBy" + orderBy.toString()).build();

	}

}

URI Pattern : “users/query


getUsers is called, from : 1000, to : 999, orderBy[name]

Download Source Code

Download it – JAX-RS-QueryParam-Example.zip (6 KB)

References

  1. JAX-RS @QueryParam JavaDoc
  2. JAX-RS @Context JavaDoc
  3. JAX-RS UriInfo JavaDoc
  4. JAX-RS @DefaultValue JavaDoc

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
15 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
14 Comment authors
SamsosavTheodorosjulioAravind Recent comment authors
newest oldest most voted
julio
Guest
julio

Macho te falta el cliente

Sam
Guest
Sam

I have a list of records to be deleted and will be passed to rest api as list input but before deleting I need to validate for each record in the list if user is authorized to delete (based on customer number he is authorized to delete). 1. How can we do that? iterate the list and validate each record.? 2. what if out of 10 record ,user is not authorized to delete? How will we handle validation message for all 3 records ? 3. As mentioned in 3 ,should we do partial delete and return a list with status… Read more »

Theodoros
Guest
Theodoros

I have noticed that you can call “users/query?foo” or even “users/query?foo=bar” and still match with “users/query”. How can you prevent this?

Aravind
Guest
Aravind

Good One..

asan
Guest
asan

Thanks for the useful post.

I want to take a list as a parameter, but the value of list do not correspond to java version list. For example @QueryParam(“orderBy”) List orderBy)
This list has the value of [age, name] as an element.

Is it possible to have the value separately. Like 1st element age and 2nd element name.

Mital
Guest
Mital

How do you accept “#’ in a QueryPaarm? As of now if I send “#” it ignores # and all the chaacters following it.

prab
Guest
prab

thanks for the video

pretbrat
Guest
pretbrat

How do you validate a query parameter? I need to validate strings and numbers to be within a certain range.

Wim
Guest
Wim

Used a lot of your examples in the past couple of years.
This is an excellent blog for beginners!

nixter
Guest
nixter

Thanks, always a good tips!

Prashant
Guest
Prashant

How can you achieve same effect in Spring REST service?

cass22
Guest
cass22

Hi,

Thanks for the useful post. How can we pass OR conditions and operators like ,!= etc in the query param?

sosav
Guest
sosav

Is there a possibility to ignore an optional @Query paramater if the value is not declared?

rat
Guest
rat
trackback
JAX-RS TUTORIALS | BUYWORKSELL

[…] JAX-RS @QueryParam example Example to get query paremeter in URI path, and also how to define an optional paramater. […]