java
41 lines · 7 steps
Calling the GitHub API with Spring WebClient
A reactive Spring service that wraps GitHub's REST API with a preconfigured WebClient and typed responses.
Explained by
highlit
1@Service
2public class GitHubClient {
3
4 private final WebClient webClient;
5
6 public GitHubClient(WebClient.Builder builder) {
7 this.webClient = builder
8 .baseUrl("https://api.github.com")
9 .defaultHeader(HttpHeaders.ACCEPT, "application/vnd.github+json")
10 .build();
11 }
12
13 public Mono<Repository> getRepository(String owner, String name) {
14 return webClient.get()
15 .uri("/repos/{owner}/{name}", owner, name)
16 .retrieve()
17 .onStatus(HttpStatusCode::is4xxClientError, response ->
18 Mono.error(new RepositoryNotFoundException(owner + "/" + name)))
19 .bodyToMono(Repository.class);
20 }
21
22 public Flux<Repository> listRepositories(String owner) {
23 return webClient.get()
24 .uri(uriBuilder -> uriBuilder
25 .path("/users/{owner}/repos")
26 .queryParam("sort", "updated")
27 .queryParam("per_page", 50)
28 .build(owner))
29 .retrieve()
30 .bodyToFlux(Repository.class);
31 }
32
33 public record Repository(
34 Long id,
35 String name,
36 @JsonProperty("full_name") String fullName,
37 @JsonProperty("html_url") String url,
38 @JsonProperty("stargazers_count") int stars,
39 @JsonProperty("private") boolean isPrivate) {
40 }
41}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Configuring a WebClient once in the constructor centralizes base URL and headers for every call.
- 2Mono and Flux let you model single versus multiple results without blocking threads.
- 3onStatus turns HTTP error codes into meaningful domain exceptions instead of generic failures.
Related explainers
java
public Map<Long, List<Order>> ordersByCustomer(List<Order> orders) { return orders.stream() .collect(Collectors.groupingBy(Order::getCustomerId)); }
Grouping streams with Java Collectors
streams
grouping
collectors
Intermediate
5 steps
rust
use axum::{ body::Bytes, extract::State, http::StatusCode,
Handling raw byte uploads in Axum
extractors
shared-state
request-limits
Intermediate
7 steps
typescript
import { Directive, Input, TemplateRef,
Building a structural *appUnless directive in Angular
structural-directive
template-rendering
dependency-injection
Intermediate
8 steps
java
@Service public class PaymentGatewayClient { private static final Logger log = LoggerFactory.getLogger(PaymentGatewayClient.class);
Resilient payment calls with Spring Retry
retry
backoff
fault-tolerance
Intermediate
7 steps
javascript
const RETRIABLE_STATUS = new Set([408, 429, 500, 502, 503, 504]); function sleep(ms, signal) { return new Promise((resolve, reject) => {
Retrying fetch with exponential backoff
retry
exponential-backoff
abort-signal
Advanced
8 steps
rust
use axum::{ extract::{Query, State}, http::StatusCode, Json,
Paginated, filtered product listing in Axum
pagination
query-parameters
sql-filtering
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/calling-the-github-api-with-spring-webclient-explained-java-1688/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.