Enterprise Integration Zone is brought to you in partnership with:

I have been a programmer since I was a young child in the Gifted and Talent Program in Rutherford, NJ back in 1979. I started programming on Commodore PETs – 4 bit. And then Apple IIs, Commodore 64s and some others. Basic, then Pascal, Fortran, C, C++, Java, JavaScript and more. I am a Senior Field Engineer with Pivotal. Tim is a DZone MVB and is not an employee of DZone and has posted 59 posts at DZone. You can read more from them at their website. View Full User Profile

Using Square Retrofit REST Client with POJO

06.05.2013
| 5290 views |
  • submit to reddit

Another helpful resource when using Retrofit, JSON to POJO

In your maven pom.xml.

<dependency>
<groupId>com.squareup.retrofit</groupId>
<artifactId>retrofit</artifactId>
<version>(insert latest version)</version>
</dependency>
package com.agilemobiledeveloper.dataradiator.dao;
import retrofit.http.GET;
import retrofit.http.Path;
import retrofit.RestAdapter;
/**
 * 
 * @author spannt
 *
 */
public class TestGitHub {
 static final String API_URL = "https://api.github.com";
static class GithubUserData{
 String avatar_url;
 String bio;
 String blog;
 String company;
 String created_at;
 String email;
 String events_url;
 Number followers;
 String followers_url;
 Number following;
 String following_url;
 String gists_url;
 String gravatar_id;
 boolean hireable;
 String html_url;
 Number id;
 String location;
 String login;
 String name;
 String organizations_url;
 Number public_gists;
 Number public_repos;
 String received_events_url;
 String repos_url;
 String starred_url;
 String subscriptions_url;
 String type;
 String updated_at;
 String url;
 }
interface GithubUser {
 @GET("/users/{username}")
 GithubUserData user(@Path("username") String username);
 }
/**
 * 
 * @param args
 */
 public static void main(String... args) {
 // Create a very simple REST adapter which points the GitHub API endpoint.
 RestAdapter restAdapter = new RestAdapter.Builder()
 .setServer(API_URL)
 .build();

 // Create an instance of our GitHub API interface.
 GithubUser githubUser = restAdapter.create(GithubUser.class);
 GithubUserData userdata = githubUser.user("nxbdi");

 System.out.println("UserData=" + userdata.bio + "," + userdata.email + "," + userdata.name +
 userdata.location + "," + userdata.public_repos + "," + userdata.followers);
 }
}

The library is really simple to use especially if you create your POJO class with the JSONGen.

Console Output:

UserData=Java, Android, NoSQL, SQL. 20 years experience. MS/BS in Com Sci.,tim@agilemobiledeveloper.com,Tim SpannNew Jersey,205,22





Published at DZone with permission of Tim Spann, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)