Thursday 10, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Saturday, 16 November 2013

JAX-RS get QueryParam using @Context UriInfo


In this tutorial we are going to see how to retrieve  query parameters in RESTful web service using @Context UriInfo
1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project .

2. Create the Project called RESTful-WebService
3. Add the following jar into WEF-INF/lib folder

4. Create package called com.javatutorialscorner.jaxrs.queryparam under RESTful-WebService
5. Create Java class QueryParamService under com.javatutorialscorner.jaxrs.queryparam package

service

QueryParamService .java

01.package com.javatutorialscorner.jaxrs.queryparam;
02. 
03.import java.util.List;
04. 
05.import javax.ws.rs.GET;
06.import javax.ws.rs.Path;
07.import javax.ws.rs.core.Context;
08.import javax.ws.rs.core.Response;
09.import javax.ws.rs.core.UriInfo;
10. 
11.@Path("/tutorials")
12.public class QueryParamService {
13. 
14. @GET
15. @Path("/java-tutorial")
16. public Response getTutorial(@Context UriInfo uriInfo) {
17.  String site = uriInfo.getQueryParameters().getFirst("site");
18.  List<String> tutorials = uriInfo.getQueryParameters().get("tutorial");
19.  String rank = uriInfo.getQueryParameters().getFirst("rank");
20.  return Response
21.    .status(200)
22.    .entity("Web Site : " + site + "  Tutorial : "
23.      + tutorials.toString() + " Rank : " + rank).build();
24. }
25.}

In above class @Context UriInfo used to get the value of URL query parameter into Java Class.

6.Configure Jersey Servlet Dispatcher
you need to configure REST as servlet in web.xml.
web.xml

01.<?xml version="1.0" encoding="UTF-8"?>
03.  <display-name>JAX-RS-Path</display-name>
04.<servlet>
05.  <servlet-name>jersey-serlvet</servlet-name>
06.  <servlet-class>
07.                     com.sun.jersey.spi.container.servlet.ServletContainer
08.                </servlet-class>
09.  <init-param>
10.       <param-name>com.sun.jersey.config.property.packages</param-name>
11.       <param-value>com.javatutorialscorner.jaxrs.queryparam</param-value>
12.  </init-param>
13.  <load-on-startup>1</load-on-startup>
14. </servlet>
15.  
16. <servlet-mapping>
17.  <servlet-name>jersey-serlvet</servlet-name>
18.  <url-pattern>/rest/*</url-pattern>
19. </servlet-mapping>
20.</web-app>

The servlet class available in jersey com.sun.jersey.spi.container.servlet.ServletContainer. The init param com.sun.jersey.config.property.package is used to define in which package jersey will look for the service classes.This package points to your resource class package. URL pattern is the part of base URL

Now you can run the service and access the service by calling the following URL

http://localhost:8080/RESTful-WebService/rest/tutorials/java-tutorial?site=www.javatutorialscorner.com&tutorial=RESTful%20WebService&tutorial=Java%20Tutorial&rank=1

you can call the service by using web browser or from client class.
Web browser call

uri info

Create Client
1.Create package called com.javatutorialscorner.jaxrs.client under RESTful-WebService project

2.Create Client Java Class RESTfulClient under  com.javatutorialscorner.jaxrs.client package


RESTfulClient .java

01.package com.javatutorialscorner.jaxrs.client;
02. 
03.import com.sun.jersey.api.client.Client;
04.import com.sun.jersey.api.client.ClientResponse;
05.import com.sun.jersey.api.client.WebResource;
06.import com.sun.jersey.api.client.config.ClientConfig;
07.import com.sun.jersey.api.client.config.DefaultClientConfig;
08. 
09.public class RESTfulClient {
10. 
11. /**
12.  * @param args
13.  */
14. public static void main(String[] args) {
15.  // TODO Auto-generated method stub
16.  String baseURI = "http://localhost:8080/RESTful-WebService";
17.  ClientConfig config = new DefaultClientConfig();
18.  Client client = Client.create(config);
19.  WebResource service = client.resource(baseURI);
20.  System.out.println("output for path param");
21.  System.out.println(service.path("rest").path("tutorials/java-tutorial")
22.    .queryParam("site", "www.javatutorialscorner.com")
23.    .queryParam("tutorial", "RESTful WebService")
24.    .queryParam("tutorial", "Java Tutorial")
25.    .queryParam("rank", "1").get(ClientResponse.class)
26.    .getEntity(String.class));
27. }
28. 
29.}

Now you can run the client and see the following output in console

output for path param

Web Site : www.javatutorialscorner.com  Tutorial : [RESTful WebService, Java Tutorial] Rank : 1

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • JAX-RS QueryParamIn this tutorial we are going to see how to pass parameters to RESTful web service via URL using @QueryParam1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project . 2. Create the Project called… Read More
  • JAX-RS @MatrixParamIn this tutorial we are going to see JAX-RS MatrixParam using  RESTful web service @MatrixParam annotationMatrixParam is set of name value pair separated by semi colon, for example name = value;1. Create new Dynamic web … Read More
  • JAX-RS @FormParamIn this tutorial we are going to see how to get HTML form parameters in java method using RESTful web service via URL using @FormParam @FormParam used to bind HTML form parameters  to java method1. Create new Dynamic we… Read More
  • Hello World RESTful Web serviceCreate new Dynamic Web Project and  give project name as RESTful_HelloWorld Copy all jar files into WEB-INF/lib folder.Select build path add jar in your build path.Create ClassCreate package called com.javatutorialscorn… Read More
  • JAX-RS @PATH for URIIn previous tutorials we have seen simple Hello World RESTful Web service using JAX-RS.In this tutorial we will we see @PATH annotation used for URI in RESTful web service.follow the steps given belowCreate new Dynamic Web Pr… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: JAX-RS get QueryParam using @Context UriInfo Rating: 5 Reviewed By: eHowToNow