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

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"
?>
02.
<
web-app
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns
=
"http://java.sun.com/xml/ns/javaee"
xmlns:web
=
"http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation
=
"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id
=
"WebApp_ID"
version
=
"3.0"
>
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
you can call the service by using web browser or from client class.
http://localhost:8080/RESTful-WebService/rest/tutorials/java-tutorial?site=www.javatutorialscorner.com&tutorial=RESTful%20WebService&tutorial=Java%20Tutorial&rank=1
Web browser call

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
0 comments:
Post a Comment