Last active
August 19, 2016 18:59
-
-
Save mosampaio/2428ebc28435b14eb6ee804bee1a1b17 to your computer and use it in GitHub Desktop.
How to consume a json Rest API with Java using Feign
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'java' | |
repositories { | |
jcenter() | |
} | |
dependencies { | |
compile 'io.github.openfeign:feign-core:9.2.0' | |
compile 'io.github.openfeign:feign-gson:9.2.0' | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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