Consuming restful web services using Unirest httpclient Java

Restful Web Services – Unirest makes httpclient java calls easy

web-services-httpclient

Fist Step is to Download Java if you don’t have already.

Let me show you how to consume webservices using unirest httpclient java; api provided by Stackoverflow to fetch any user information on their website.

Open eclipse mars or any IDE you are familiar with. For this article, let me assume you have eclipse as your IDE.

Right click on the Project Explorer and click New. Select Other and type Maven. Select Maven Project as your option and click next.

Enter the location of your workspace and click next. On the next screen if you are prompted for Archetype, select maven-archetype-quickstart 1.1 and hit next.

Group id could be anything say com.unirest & artifact id could be httpclient and hit finish.

A new Maven project structure will be created with your artifact id httpclient. Open the pom.xml file within that project to edit.

Just before the closing </project> tag you can add this dependency.

<dependencies>
...
<dependency>
<groupId>com.mashape.unirest</groupId>
<artifactId>unirest-java</artifactId>
<version>1.4.8</version>
</dependency>
...
</dependencies>

In the httpclient project folder you will notice below structure

src/main/java

Open the package com.unirest.httpclient and see the java file.

Update your java file as below

package com.unirest.httpclient;

import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;

/**
* Unirest Client !
*
*/
public class App {
public void UnirestClient() throws Exception {

HttpResponse<JsonNode> stack = Unirest.get("https://api.stackexchange.com/2.2/users/387774")
.header("accept", "application/json").queryString("filter", "default")
.queryString("site", "stackoverflow").asJson();
System.out.println(stack.getBody().getObject().toString(2));

}

public static void main(String[] args) throws Exception {
App client = new App();
client.UnirestClient();

}
}

Select your project and hit ‘Alt+F5’. This way Maven will import all your required jars to run the project.

Select your Java file and right click to hit Run As – Java Application.

Notice that Stackoverflow has given JSON response to your webservice request to fetch user 387774 (Stratageeks User) information.

{
"quota_max": 300,
"items": [{
"last_access_date": 1478157060,
"account_id": 164781,
"accept_rate": 73,
"user_type": "registered",
"profile_image": "https://www.gravatar.com/avatar/d0f131a1ed3db917bdfe5567758a755f?s=128&d=identicon&r=PG&f=1",
"link": "http://stackoverflow.com/users/387774/some-java-guy",
"reputation_change_week": 10,
"reputation_change_day": 0,
"badge_counts": {
"bronze": 72,
"silver": 42,
"gold": 9
},
"creation_date": 1278682709,
"display_name": "Some Java Guy",
"is_employee": false,
"last_modified_date": 1470224611,
"reputation": 1533,
"reputation_change_year": 298,
"website_url": "http://www.stratageeks.com",
"user_id": 387774,
"reputation_change_month": 5,
"reputation_change_quarter": 42
}],
"has_more": false,
"quota_remaining": 295
}

Yay! You have created your first HttpClient to hit third party webservice.

One thought on “Consuming restful web services using Unirest httpclient Java

Leave a Reply

Your email address will not be published. Required fields are marked *