Created
August 19, 2016 19:07
-
-
Save mosampaio/65aab9ced5e344423e3165f7ee7e36c3 to your computer and use it in GitHub Desktop.
How to consume a json Rest API with Java using Spring
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 'com.fasterxml.jackson.core:jackson-databind:2.8.1' | |
compile 'org.springframework:spring-web:4.3.2.RELEASE' | |
} |
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 com.fasterxml.jackson.annotation.JsonCreator; | |
import com.fasterxml.jackson.annotation.JsonProperty; | |
import org.springframework.web.client.RestTemplate; | |
import java.util.List; | |
import static java.util.Arrays.asList; | |
public class SpringLibrary { | |
static class Contributor { | |
String login; | |
int contributions; | |
@JsonCreator | |
Contributor(@JsonProperty("login") String login, @JsonProperty("contributions") int contributions) { | |
this.login = login; | |
this.contributions = contributions; | |
} | |
public String toString() { return login + " (" + contributions + ")"; } | |
} | |
public static void main(String[] args) { | |
String url = "https://api.github.com/repos/twilio/twilio-java/contributors"; | |
RestTemplate restTemplate = new RestTemplate(); | |
List<Contributor> contributors = asList(restTemplate.getForObject(url, Contributor[].class)); | |
contributors.stream().forEach(System.out::println); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice one Bro.