Skip to content

Instantly share code, notes, and snippets.

@clonekim
Created November 1, 2019 00:43
Show Gist options
  • Save clonekim/494ee1e8af162865270f07c41ceb665f to your computer and use it in GitHub Desktop.
Save clonekim/494ee1e8af162865270f07c41ceb665f to your computer and use it in GitHub Desktop.
Spring + RestTemplate
@Service
@GraphQLApi
public class GitHubService {
@Autowired
RestTemplate restTemplate;
@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
@JsonIgnoreProperties(ignoreUnknown = true)
@Getter
@Setter
static class GitHubRepoistory {
@JsonProperty("id")
int id;
@JsonProperty("full_name")
String fullName;
}
@GraphQLQuery(name = "repos")
public Map<String, Object> getRepository(@NotEmpty String username) throws URISyntaxException {
String url = String.format("https://api.github.com/users/%s/repos", username);
GitHubRepoistory[] repoistoriest = restTemplate.getForObject(new URI(url), GitHubRepoistory[].class);
List<Map<String, Object>> list = Arrays.stream(repoistoriest).map( i -> new HashMap<String, Object>(){{
put("id", i.id);
put("fullName", i.fullName);
}}).collect(Collectors.toList());
return new HashMap<String, Object>() {{
put("repos", list);
put("count", list.size());
}};
/* AS Same
ResponseEntity<List<GitHubRepoistory>> response = restTemplate.exchange(url, HttpMethod.GET, null, new ParameterizedTypeReference<List<GitHubRepoistory>>() {});
return response.getBody().stream().map( i -> new HashMap<String, Object>(){
{
put("id", i.id);
put("fullName", i.fullName);
}}).collect(Collectors.toList());
*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment