Skip to content

Instantly share code, notes, and snippets.

@mosampaio
Last active August 19, 2016 18:59
Show Gist options
  • Save mosampaio/2428ebc28435b14eb6ee804bee1a1b17 to your computer and use it in GitHub Desktop.
Save mosampaio/2428ebc28435b14eb6ee804bee1a1b17 to your computer and use it in GitHub Desktop.
How to consume a json Rest API with Java using Feign
apply plugin: 'java'
repositories {
jcenter()
}
dependencies {
compile 'io.github.openfeign:feign-core:9.2.0'
compile 'io.github.openfeign:feign-gson:9.2.0'
}
import feign.Feign;
import feign.Param;
import feign.RequestLine;
import feign.gson.GsonDecoder;
import java.util.List;
class FeignLibrary {
static class Contributor {
String login;
int contributions;
public String toString() { return login + " (" + contributions + ")"; }
}
interface GitHub {
@RequestLine("GET /repos/{owner}/{repo}/contributors")
List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo);
}
public static void main(String... args) {
GitHub github = Feign.builder()
.decoder(new GsonDecoder())
.target(GitHub.class, "https://api.github.com");
List<Contributor> contributors = github.contributors("twilio", "twilio-java");
contributors.stream().forEach(System.out::println);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment