JAX-RS @QueryParam. Reading URL Query Parameters.

@QueryParam annotation allows us to read the request parameter values which were passed as a part of URL query string for example:

site.com/api/users/r4ghtaf43c3n/messages?start=1&limit=50 where

site.com is your web site domain name,
/api/users/ is the path to your Root Resource,
r4ghtaf43c3n is the value of specific user id and can be read with @ParthParam annotation,
/messages is the path to a sub-resource which is called “/messages”,
and ?start=1&limit=50 is the url query string.

Query parameters start with ? and are separated by &. So the first @QueryParam will be the “start” and its value is 1 and the second @QueryParam will be the “limit” and it’s value is 50.

Notice that there is a big difference between @PathParam and @QueryParam annotations and you cannot use @PathParam to read the query parameters. Please refer to JAX-RS @PathParam. Reading Path Parameters example to learn how to read path parameters values.

JAX-RS @QueryParam example. Reading Request Parameters Passed via the URL Query String.

Below is a code example which demonstrates how to read url query parameters using the @PathParam annotation. Please also notice that you can use the @DefaultValue annotation to specify a default value of a query parameter. If the query parameter is missing in the url query string but your code is expecting it the value specified with the @DefaultValue annotation will be used.

    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public List<UserProfile> getProfiles(@DefaultValue("0") @QueryParam("start") int start,
            @DefaultValue("50") @QueryParam("limit") int limit) {
        ProfileService profileService = new ProfileServiceImpl();
        // We can now use the values of start and limit query parameters. 
        List<ProfileDto> profiles = profileService.getUserProfiles(start, limit);
        return returnValue;

If you are interested to learn more about building RESTful web services with JAX-RS and Jersey, please check other tutorials on this web site or checkout the three video courses I am listing below which will help you greatly speed up your learning progress.

Happy learning! 🙂

Java Web Services. Video Course.

Learn how to design,create , consume and secure SOAP and REST web services from scratch in easy steps.


Java Web Services Part 2. Video Course.

Master advanced web services concepts and implement them in easy steps

Java Web Services Part 2

REST Java Web Services. Video Course.

A guide to understanding, accessing, and writing a REST Java web service using Apache and Java EE.

Java Web Services Part 2

Leave a Reply

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