Newsletter |
RESTful Web Services (JAX-RS) @QueryParam Example
Web Services » on Jul 8, 2014 { 6 Comments } By Sivateja
In RESTful web services (JAX-RS) @QueryParam annotation will be used to get the query parameters from the URL, Observe carefully, i am saying we will retrieve the parameters only not their values. But in case of @PathParam we will get parameter values directly.
Query Parameters Syntax
Consider this URL:
http://localhost:2013/RestPathAnnotationExample/rest/customers?nameKey=Java4s&countryKey=USA
Here query parameters are nameKey, countryKey and their values are Java4s, USA respectively, hope you understood.
Required Files
- pom.xml & web.xml are similar to previous article no changes 🙂
- RestServiceQueryParamJava4s.java
RestServiceQueryParamJava4s.java
package com.java4s; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Response; @Path("/customers") public class RestServiceQueryParamJava4s { @GET @Produces("text/html") public Response getResultByPassingValue( @QueryParam("nameKey") String name, @QueryParam("countryKey") String country) { String output = "Customer name - "+name+", Country - "+country+""; return Response.status(200).entity(output).build(); } }
Explanation
- Right click on your project folder > Run As > Run on Server
- Eclipse will open http://localhost:2013/RestQueryParamAnnotationExample/ with HTTP 404 Error
- In web.xml we have specified the url pattern as /rest/* (line number 19) and in RestServiceQueryParamJava4s.java we specified class level @path as /customers [ line number 9 ] and we are retrieving 2 query parameters [ Line number 15,16 ], so our final URL should be the http://localhost:2013/RestQueryParamAnnotationExample/rest/customers?nameKey=java4s&countryKey=USA
- Check the output by hitting the above URL
Output
You Might Also Like
::. About the Author .:: | ||
Comments
6 Responses to “RESTful Web Services (JAX-RS) @QueryParam Example”
Hai,
Sivateja Garu,your examples and explanation is very nice,this is very right tutorial for beginners.
But we need JSON resource from rest service and also we all looking soap based web service.
thanks®ards,
Satish.
@Satish
We will finish JAX-RS by end of this week, this will cover everything including JSON resource style.
-Siva
Hi,
Am getting “HTTP Status 500 – Servlet.init() for servlet jersey-serlvet threw exception” when i try to use @QueryParam. And it is working fine for @PathParam.
What i need to do?
-Thanks.
Hi Sive,
Thank u very much for make me a clean understanding of ur tutorials .
I start to learn angualr js.
I need a small example in angular js with image upload and display the uploaded image download.
Waiting for u r response.
Thanks & Regards
Satish Marutham
@SatishReddy,
We are currently working on completing the AngularJS, yep definitely we will consider this scenario and post an article on it.
hi anna how to retreving image from my sql database in java with jdbc code send example pze anna