RESTful Web Services (JAX-RS) @QueryParam Example

Last updated on March 14th, 2017 at 07:31 am

Previous
Next
This post is in continuation with web service tutorial (Part -9).

In previous post, we have used @PathParam for passing parameter to URL in RESTful Web Services json example and we have directly passed parameter values but JAXRS @QueryParam can be used to pass query parameters. In case of @QueryParam, we pass parameters as well as its values.
For example: http://localhost:8080/JAXRSJsonExample/rest/countries/country?id=3&countryName=Nepal
Here Parameter names are id,countryName and its values are 3,Nepal respectively.

@QueryParam can be used in case of filtering . When you want show both param as well value in url.
For example: Lets say you are showing list of customers on a website and you can show 10 at a time. When user click next , he will see next 10 customers, so in this case, we can use @QueryParam may look something like this

http://localhost:8080/JAXRSJsonExample/rest/listOfCustomers?start=10&size=10

Here are steps to create a JAXRS json example which will use @QueryParam.

1) Follow steps on RESTful Web Services json example to create simple RESTful web services which uses @PathParam.
2) Change CountryRestService.java to use @QueryParam as below.

That’s all. We need to run the application now.

Run the application

3) Right click on project -> run as -> run on server
Select apache tomcat and click on finish
4) Now pass country id and country name as parameter to url.
“http://localhost:8080/JAXRSJsonExample/rest/countries/country?id=3&countryName=Nepal”.

We are done with Restful web services json QueryParam example. If you are still facing any issue, please comment.

Previous
Next

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.

One Response

  1. Jitendra February 25, 2016

Add Comment

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.
You can like our facebook page Java2blog