Accessing a REST api with the Jersey Client API with JDeveloper and ADF Faces

This is a guest post by my coworker Mark Yvanovich.

Use Case Background

Representational state transfer (REST) is a common, widely used software architecture for distributing data over the WWW. Many sites such as twitter, facebook and google provide access to their internal data via RESTful APIs. This wiki page shows how to easily access RESTful data in an ADF Faces application.

Using the Jersey Client API

There is no automatic way to access data from a REST XML api in Jdeveloper, but using the Jersey Client api makes it relatively easy. The Jersey Client api jars are available here:
https://jersey.dev.java.net/nonav/documentation/latest/user-guide.html#core_client

Let's start with a sample REST api URL:
http://twitter.com/statuses/public_timeline.xml

This is some sample output if you enter this URL in a browser:

  1 <statuses type="array">
  2  <status>
  3   <created_at>Fri Jan 29 18:20:12 +0000 2010</created_at>
  4   <id>8378218352</id>
  5   <text>
  6     @fastbreak_nash some people always run late, 
  7     can't fix it with some people ( my family has a few)
  8   </text>
  9   <source>
 10    <a href="http://www.tweetdeck.com/" rel="nofollow">TweetDeck</a>
 11   </source>
 12   <truncated>false</truncated>
 13   <in_reply_to_status_id>8378149695</in_reply_to_status_id>
 14   <in_reply_to_user_id>30243874</in_reply_to_user_id>
 15   <favorited>false</favorited>
 16   <in_reply_to_screen_name>fastbreak_nash</in_reply_to_screen_name>
 17   <user>
 18    <id>23691253</id>
 19    <name>Kathryn Van Gompel</name>
 20    <screen_name>kitkatflute</screen_name>
 21    <location>Mesa, AZ</location>
 22    <description>Phoenix Suns fan, like to quilt & enjoy movies</description>
 23    <profile_image_url>
 24      http://a3.twimg.com/profile_images/573592159/twitterProfilePhoto_normal.jpg
 25    </profile_image_url>
 26    <url/>
 27    <protected>false</protected>
 28    <followers_count>223</followers_count>
 29    <profile_background_color>000000</profile_background_color>
 30    <profile_text_color>1f1f1f</profile_text_color>
 31    <profile_link_color>cccccc</profile_link_color>
 32    <profile_sidebar_fill_color>111111</profile_sidebar_fill_color>
 33    <profile_sidebar_border_color>ffffff</profile_sidebar_border_color>
 34    <friends_count>167</friends_count>
 35    <created_at>Wed Mar 11 00:35:32 +0000 2009</created_at>
 36    <favourites_count>1</favourites_count>
 37    <utc_offset>-28800</utc_offset>
 38    <time_zone>Pacific Time (US & Canada)</time_zone>
 39    <profile_background_image_url>
 40      http://a3.twimg.com/profile_background_images/50062907/1534_bg.jpg
 41    </profile_background_image_url>
 42    <profile_background_tile>false</profile_background_tile>
 43    <notifications>false</notifications>
 44    <geo_enabled>false</geo_enabled>
 45    <verified>false</verified>
 46    <following>false</following>
 47    <statuses_count>6185</statuses_count>
 48   </user>
 49   <geo/>
 50 </status>
 51 ...
 52 </statuses>
You can see that there are two data structures, 'status' and 'user' (which is a child of status), so we will need to make two beans for these structures. Let's call them TwitterStatus and TwitterUser. First, lets create the annotated beans:
  1 @XmlRootElement(name="status")
  2 public class TwitterStatus
  3 {
  4   @XmlElement(name="created_at") private String _createdAt;
  5   @XmlElement(name="id") private String _id;
  6   @XmlElement(name="text") private String _text;
  7   @XmlElement(name="source") private String _source;
  8   @XmlElement(name="truncated") private String _truncated;
  9   @XmlElement(name="in_reply_to_status_id") private String _inReplyToStatusId;
 10   @XmlElement(name="in_reply_to_user_id") private String _inReplyToUserId;
 11   @XmlElement(name="favorited") private String _favorited;
 12   @XmlElement(name="in_reply_to_screen_name") private String _inReplytoScreenName;
 13   @XmlElement(name="user") private TwitterUser _user;
 14   @XmlElement(name="geo") private String _geo;
 15 }
and
  1 @XmlRootElement(name="user")
  2 public class TwitterUser
  3 {
  4   @XmlElement(name="id") private String _id;
  5   @XmlElement(name="name") private String _name;
  6   @XmlElement(name="screen_name") private String _screenName;
  7   @XmlElement(name="location") private String _location;
  8   @XmlElement(name="description") private String _description;
  9   @XmlElement(name="profile_image_url") private String _profileImageUrl;
 10   @XmlElement(name="url") private String _url;
 11   @XmlElement(name="protected") private String _protected;
 12   @XmlElement(name="followers_count") private String _followersCount;
 13   @XmlElement(name="profile_background_color") private String _profileBackgroundColor;
 14   @XmlElement(name="profile_text_color") private String _profileTextColor;
 15   @XmlElement(name="profile_link_color") private String _profileLinkColor;
 16   @XmlElement(name="profile_sidebar_fill_color") private String _profileSidebarFillColor;
 17   @XmlElement(name="profile_sidebar_border_color") private String _profileSidebarBorderColor;
 18   @XmlElement(name="friends_count") private String _friendsCount;
 19   @XmlElement(name="created_at") private String _createdAt;
 20   @XmlElement(name="favorites_count") private String _favoritesCount;
 21   @XmlElement(name="utc_offset") private String _utcOffest;
 22   @XmlElement(name="time_zone") private String _timeZone;
 23   @XmlElement(name="profile_background_image_url") private String _profileBackgroundImageUrl;
 24   @XmlElement(name="profile_background_title") private String _profileBackgroundTile;
 25   @XmlElement(name="geo_enabled") private String _geoEnabled;
 26   @XmlElement(name="verified") private String _verified;
 27   @XmlElement(name="following") private String _following;
 28   @XmlElement(name="statuses_count") private String

You can right click on the bean data element in Jdev to generate the accessors.

Now all we need to do is create the backing bean with a method for retrieving the data:

  1 public class TwitterBean
  2 {
  3   public List<TwitterStatus> getPublicStatusFeed()
  4   {
  5     // create the Jersey client
  6     Client c = Client.create();
  7 
  8     // set the base URI
  9     WebResource wr = c.resource(BaseURI);
 10 
 11     // set up and make the REST api request.
 12     return wr.path("statuses/public_timeline.xml").accept(MediaType.APPLICATION_XML_TYPE).get(new GenericType<List<TwitterStatus>>() {});
 13     // or, if the REST api returns JSON data, you could would change your call to:
 14     // return wr.path("statuses/public_timeline.json").accept(MediaType.APPLICATION_JSON_TYPE).get(new GenericType<List<TwitterStatus>>() {});
 15   }
 16 
 17   private static final String BaseURI = "https://twitter.com";
 18 }
Now you can register the TwitterBean and use it in an EL expression. Here's an example:
  1 <af:iterator var="row" varStatus="stat" value="#{twitter.publicStatusFeed}" id="itr">
  2   ...
  3 </af:iterator>
Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

An exploration in the lighter side of ADF development.

Search

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