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

16 comments on “JAX-RS @QueryParam example

  1. 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.

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

    Reply
  3. 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?

    Reply
  4. 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.

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

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

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

    Reply
  8. How can you achieve same effect in Spring REST service?

    Reply
  9. Hi,

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

    Reply

Leave a Comment

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