Social Media

Retrofit – RESTful Services made Easy

This is more of a bookmark post. But Ive been looking at some RESTful API’s recently and trying to decide the best approach – which was most likely Springs REST template. I then found RetrofitRetrofit as an alternative.

Its syntax is pretty simple, and it basically maps the JSON response to the Java object, and all you are required to do is define the interface. Similar to Spring Data.

The example given on the retrofit website demonstrates this –

public interface GitHubService {
@GET("/users/{user}/repos")
List listRepos(@Path("user") String user);
}

Then to call the code –

RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("https://api.github.com")
.build();

GitHubService service = restAdapter.create(GitHubService.class);

List repos = service.listRepos("octocat");

I’ll definitely consider this in future projects

About the Author Martin Farrell

My name is Martin Farrell. I have almost 20 years Java experience. I specialize inthe Spring Framework and JEE. I’ve consulted to a range of businesses, and have provide Java and Spring mentoring and training. You can learn more at About or on my consultancy website Glendevon Software

follow me on:

Leave a Comment: