Unravel › Java concepts › APIs and web
HTTP clients
An HTTP client calls another service over the network.
Learn these first
At a glance
Phoning another office: decide how long you will wait for them to pick up and how long for an answer, and what you do if they say "busy" or never reply.
- 1Java's built-in HttpClient builds a HttpRequest and calls send (blocking) or sendAsync (returns a CompletableFuture)Spring offers RestTemplate (blocking), RestClient (modern blocking) and WebClient (reactive).
- 2A response has a status code and a bodyA 404 or 500 is not an exception with the plain Java client; you must check statusCode(). Spring clients throw on 4xx and 5xx by default.
- 3Always set a connect timeout and a request or read timeoutWithout them a slow dependency can hold your threads forever.
- Reuse client instances (they hold connection pools)
- Connections are pooled and reused with keep-alive; HTTP/2 multiplexes several requests over one connection
HttpClient client = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(3)).build();HttpRequest request = HttpRequest.newBuilder(URI.create("https://api.example.com/items/7")) .timeout(Duration.ofSeconds(5)).GET().build();HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());if (response.statusCode() != 200) { throw new IllegalStateException("unexpected status " + response.statusCode());}- line 1 uses HttpClient, the core of HTTP clients
- line 2 uses HttpRequest, the core of HTTP clients
- line 4 uses HttpResponse, the core of HTTP clients
Read the full explanation (about 1 min)
An HTTP client calls another service over the network. The key things to get right are timeouts, error status handling, and not blocking threads you cannot afford to block.
A mental model
Phoning another office: decide how long you will wait for them to pick up and how long for an answer, and what you do if they say "busy" or never reply.
How it works
Java's built-in HttpClient builds a HttpRequest and calls send (blocking) or sendAsync (returns a CompletableFuture). Spring offers RestTemplate (blocking), RestClient (modern blocking) and WebClient (reactive).
A response has a status code and a body. A 404 or 500 is not an exception with the plain Java client; you must check statusCode(). Spring clients throw on 4xx and 5xx by default.
Always set a connect timeout and a request or read timeout. Without them a slow dependency can hold your threads forever.
Going deeper
Reuse client instances (they hold connection pools). Retry only idempotent requests, with backoff and a limit, and consider a circuit breaker so a failing dependency does not drag your service down. Never log full request bodies containing credentials.
Under the hood
Connections are pooled and reused with keep-alive; HTTP/2 multiplexes several requests over one connection. A blocking client parks the calling thread while waiting for the response.
Common mistakes
- No timeouts.
- Creating a new client for every request.
- Retrying non-idempotent calls blindly.
- Ignoring status codes.
How Unravel recognises it
When you paste code, Unravel looks for these signs of http clients. Spotting them yourself is a good habit when you read unfamiliar code:
- Annotations:
@FeignClient - Types:
HttpClientHttpRequestHttpResponseRestTemplateWebClientRestClientOkHttpClientHttpURLConnectionFeignClientCloseableHttpClient
Related concepts
Builds on this
Frequently asked questions
What is HTTP clients?
An HTTP client calls another service over the network. The key things to get right are timeouts, error status handling, and not blocking threads you cannot afford to block.
What are the most common mistakes with HTTP clients?
No timeouts. Creating a new client for every request. Retrying non-idempotent calls blindly. Ignoring status codes.
How can I see HTTP clients in real code?
Paste your own Java into Unravel and it will point out where http clients appears, explain how it works at the detail level you choose, and draw the flow of the code around it.