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
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Spring can generate a working HTTP client from an interface, so you describe endpoints instead of writing request plumbing.
- 2Method annotations like @GetExchange and @PathVariable map Java signatures directly onto HTTP verbs, paths, and parameters.
- 3Centralizing base URL and auth headers in the WebClient keeps per-call methods clean and consistent.
Related explainers
java
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;
Building a trie for autocomplete in Java
trie
prefix-tree
recursion
Intermediate
8 steps
java
@RestController @RequestMapping("/api/products") public class ProductSearchController {
Binding collection query params in Spring
rest-api
query-parameters
dependency-injection
Intermediate
6 steps
python
import os from pathlib import Path BASE_DIR = Path(__file__).resolve().parent.parent.parent
How a Django settings module is wired
configuration
environment-variables
middleware
Intermediate
8 steps
java
import java.util.ArrayDeque; import java.util.Deque; import java.util.Map;
Evaluating math expressions with two stacks
stacks
parsing
operator-precedence
Intermediate
9 steps
java
@Entity @Table(name = "orders") @SQLDelete(sql = "UPDATE orders SET deleted = true, deleted_at = now() WHERE id = ?") @Where(clause = "deleted = false")
Soft deletes with Hibernate in Spring
soft-delete
jpa
hibernate
Intermediate
9 steps
java
@GetMapping("/files/{id}") public ResponseEntity<StreamingResponseBody> download( @PathVariable String id, @RequestHeader(value = HttpHeaders.RANGE, required = false) String rangeHeader) throws IOException {
HTTP range requests in Spring
http-range
streaming
file-io
Advanced
10 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/declarative-http-clients-in-spring-explained-java-00ab/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.