HTTP Client and REST Fundamentals

Modern Java includes an HTTP client API for synchronous and asynchronous HTTP calls.

HttpClient client = HttpClient.newHttpClient();

HttpRequest request = HttpRequest.newBuilder()
    .uri(URI.create("https://example.com/api/users"))
    .GET()
    .build();

HttpResponse<String> response =
    client.send(request, HttpResponse.BodyHandlers.ofString());

A production HTTP client needs timeouts, connection reuse, retries where safe, observability, response validation, and protection against unbounded concurrency.

Key Takeaways
  • HTTP clients need timeouts and resilience policies.
  • Retries are only safe when operation and server semantics allow them.
  • API contracts should make validation and error behavior explicit.