java 41 lines · 8 steps

Declarative HTTP clients in Spring

An annotated interface becomes a live GitHub API client through Spring's HttpServiceProxyFactory.

Explained by highlit
1public interface GitHubClient {
2 
3 @GetExchange("/users/{username}")
4 GitHubUser getUser(@PathVariable String username);
5 
6 @GetExchange("/users/{username}/repos")
7 List<Repository> listRepositories(@PathVariable String username,
8 @RequestParam(defaultValue = "updated") String sort,
9 @RequestParam(name = "per_page", defaultValue = "30") int perPage);
10 
11 @PostExchange("/user/repos")
12 Repository createRepository(@RequestHeader("Authorization") String bearerToken,
13 @RequestBody CreateRepositoryRequest request);
14 
15 @DeleteExchange("/repos/{owner}/{repo}")
16 ResponseEntity<Void> deleteRepository(@PathVariable String owner, @PathVariable String repo);
17}
18 
19@Configuration
20class GitHubClientConfig {
21 
22 @Bean
23 GitHubClient gitHubClient(WebClient.Builder builder,
24 @Value("${github.base-url:https://api.github.com}") String baseUrl,
25 @Value("${github.token}") String token) {
26 WebClient webClient = builder
27 .baseUrl(baseUrl)
28 .defaultHeader(HttpHeaders.ACCEPT, "application/vnd.github+json")
29 .defaultHeader(HttpHeaders.AUTHORIZATION, "Bearer " + token)
30 .defaultHeader("X-GitHub-Api-Version", "2022-11-28")
31 .build();
32 
33 WebClientAdapter adapter = WebClientAdapter.create(webClient);
34 
35 HttpServiceProxyFactory factory = HttpServiceProxyFactory
36 .builderFor(adapter)
37 .build();
38 
39 return factory.createClient(GitHubClient.class);
40 }
41}
01 / 01
STEP 01

Walkthrough

Space play ←→ step click any line
Three takeaways
  1. 1Spring can generate a working HTTP client from an interface, so you describe endpoints instead of writing request plumbing.
  2. 2Method annotations like @GetExchange and @PathVariable map Java signatures directly onto HTTP verbs, paths, and parameters.
  3. 3Centralizing base URL and auth headers in the WebClient keeps per-call methods clean and consistent.

Related explainers

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
java
package com.acme.billing.config;
 
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;

Feature-flagged beans with Spring @ConditionalOnProperty

feature-flags conditional-beans strategy-pattern
Intermediate 5 steps
java
public static Map<String, String> parseCookieHeader(String header) {
    Map<String, String> cookies = new LinkedHashMap<>();
    if (header == null || header.isBlank()) {
        return cookies;

Parsing an HTTP Cookie header in Java

string-parsing http url-decoding
Intermediate 6 steps
java
public class TimedSocketReader {
 
    private static final int READ_TIMEOUT_MS = 5_000;
    private static final int CONNECT_TIMEOUT_MS = 3_000;

Reading a socket with connect and read timeouts

sockets timeouts io
Intermediate 8 steps
java
public final class EncodingDetector {
 
    public enum Encoding {
        UTF_8, UTF_16LE, UTF_16BE, UTF_32LE, UTF_32BE, ASCII, UNKNOWN

Detecting text encoding from raw bytes in Java

byte-manipulation encoding-detection bitwise-operations
Intermediate 8 steps

Share this explainer

Here's the card — post it anywhere.

Declarative HTTP clients in Spring — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code