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

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

6 Responses to “RESTful Web Services (JAX-RS) @QueryParam Example”
  1. satish says:

    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&regards,
    Satish.

  2. Java4s says:

    @Satish

    We will finish JAX-RS by end of this week, this will cover everything including JSON resource style.

    -Siva

  3. Arunkumar Papena says:

    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.

  4. SatishReddy says:

    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

  5. Siva says:

    @SatishReddy,

    We are currently working on completing the AngularJS, yep definitely we will consider this scenario and post an article on it.

  6. kumar says:

    hi anna how to retreving image from my sql database in java with jdbc code send example pze anna

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.