Main Tutorials

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 Author

author image
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

Subscribe
Notify of
16 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Sam
5 years ago

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 for each records? success for deleted and error message for the records user is not authorized to ?

Please advise …and if you can post a sample code will help.

asan
8 years ago

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.

gamer
3 years ago

Nice explanation. Thank you.

Jero
4 years ago

Thank you!

Theodoros
6 years ago

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?

julio
6 years ago

Macho te falta el cliente

Aravind
8 years ago

Good One..

Mital
8 years ago

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

prab
9 years ago

thanks for the video

pretbrat
9 years ago

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

Wim
10 years ago

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

nixter
11 years ago

Thanks, always a good tips!

Prashant
11 years ago

How can you achieve same effect in Spring REST service?

cass22
12 years ago

Hi,

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

sosav
5 years ago

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

rat
9 years ago