The Jersey Client API

                One of the distinguishing features of Jersey is its client API. The Jersey client API makes it really easy to consume the RESTful Web Services - send requests and get back response along with all the required entities. There are two variants of the API - one based on HttpUrlConnection and the other based on the Apache HTTP Client. The API docs (for Jersey version 1.0.2, latest as of the day) are available at:

Steps for consuming a service using the client API:

  • Get an instance of Client by either:
    1. Client client = Client.create(); for the HttpURLConnection based client, or
    2. ApacheHttpClient client = ApacheHttpClient.create(); for the ApacheHttpClient.
  • Get a handle to the resources using the resource(..) method as - WebResource webResource = client.resource(base_url_of_the_service); The base_url could be given either as a String or as java.net.URI.
  • Set the relative path to the required resource using the path() method of the WebResource class
    • Something like webResource.path(path1).path(path2) - "path1" and "path2" are the path elements. Say, I have my web service running at the base URI http://myserver/base, and the resource is identified by http://myserver/base/resources/resource1, then we could set the path to resource using the above as - webResource.path("resources").path("resource1")
    • WebResource follows the builder design pattern for appending path elements to the request URI.
  • Methods mapping each of the four HTTP methods GET, POST, PUT and DELETE are provided. These are:
    • GET ==> get()
    • POST ==> post()
    • PUT ==> put()
    • DELETE ==> delete()
  • If you want to send a GET request to a resource, that can be done using this statement:
    • webResource.path(path1).path(path2).get(Class), where Class is the type of the variable in which the response is to be set to. For example, if the response is to be set to a String variable, it could be done by using  -                                                                                                  String reponse = webResource.path(path1).path(path2).get(String.class);
    • In general, the class is set to Response.class and the response is returned as an object of type com.sun.jersey.api.client.ClientResponse.
  • If you want to send a request with the Accept Header set to some mime-type, that can be done using the accept() method of the WebResource class as shown in the following statement:
    • webResource.path(path1).path(path2).accept(mime-type).get(Class)
  • On the other hand if you want to send some data of some mime-type, it could be achieved using the type() method of the WebResource class as shown in the following statement:
    • webResource.path(path1).path(path2).type(mime-type).post(Object);

     This gives a brief introduction to the Client API. In the next entry, we will use this API to send requests to the service(s) created in the previous entries which used PathParam and QueryParam.

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

Naresh worked at Sun Microsystems for two years. During these two years he had worked on the Project Metro and Project Jersey.

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today