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
typescript
import { registerLocaleData } from '@angular/common'; import localeFr from '@angular/common/locales/fr'; import localeFrExtra from '@angular/common/locales/extra/fr'; import localeDe from '@angular/common/locales/de';
Locale-aware bootstrapping in Angular
i18n
localization
dependency-injection
Intermediate
8 steps
rust
use serde::Deserialize; #[derive(Debug, Deserialize)] #[serde(untagged)]
Parsing flexible JSON shapes with serde
deserialization
enums
json
Intermediate
6 steps
ruby
require "shellwords" require "open3" module Backup
Building safe shell commands in Ruby
shell-out
subprocess
command-injection
Intermediate
7 steps
typescript
import { Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; import * as Joi from 'joi';
Validating env config at boot in NestJS
configuration
schema-validation
environment-variables
Intermediate
8 steps
java
@Component @Converter public class EncryptedStringConverter implements AttributeConverter<String, String> {
Transparent column encryption in Spring & JPA
encryption
aes-gcm
jpa-converter
Advanced
10 steps
typescript
import { Inject, Injectable, Logger } from '@nestjs/common'; import { CACHE_MANAGER } from '@nestjs/cache-manager'; import { Cache } from 'cache-manager'; import { InjectRepository } from '@nestjs/typeorm';
A cache-aside country lookup in NestJS
cache-aside
dependency-injection
batch-lookup
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.