Java HttpClient: Built-in HTTP/2, No Dependencies, Virtual Thread Ready

🧠 𝗧𝗵𝗲 𝗛𝗶𝗱𝗱𝗲𝗻 𝗠𝗲𝗺𝗼𝗿𝘆 𝗦𝗶𝗻𝗸 (𝗣𝗮𝗿𝘁 𝟭𝟯.𝟱.𝟯): 𝗝𝗮𝘃𝗮 𝗛𝘁𝘁𝗽𝗖𝗹𝗶𝗲𝗻𝘁 - 𝗠𝗼𝗱𝗲𝗿𝗻 𝗦𝘁𝗮𝗰𝗸 𝗡𝗼𝗯𝗼𝗱𝘆 𝗨𝘀𝗲𝘀 Java 11 gave us HttpClient. HTTP/2 built-in. Connection pooling included. Async API. No OkHttp, Apache, or Netty dependency. Five years later, everyone still adds external HTTP libraries. Why? Spring Boot doesn't default to it. Teams don't know it exists. Legacy code stays legacy. But it's there. Java 21 virtual threads change blocking model. Time to reconsider built-in? --- 🔧 𝗪𝗵𝗮𝘁 𝗝𝗮𝘃𝗮 𝗛𝘁𝘁𝗽𝗖𝗹𝗶𝗲𝗻𝘁 𝗣𝗿𝗼𝘃𝗶𝗱𝗲𝘀 _HttpClient client = HttpClient.newBuilder()   .version(Version.HTTP_2) // HTTP/2 default   .connectTimeout(...)   .build();_ 𝗕𝘂𝗶𝗹𝘁-𝗶𝗻: - HTTP/2 by default (fallback to HTTP/1.1) - Connection pooling (automatic) - Async and sync APIs (CompletableFuture) - No external dependencies --- 🔧 𝗪𝗵𝘆 𝗡𝗼𝗯𝗼𝗱𝘆 𝗨𝘀𝗲𝘀 𝗜𝘁 𝗦𝗽𝗿𝗶𝗻𝗴 𝗕𝗼𝗼𝘁 𝗱𝗲𝗳𝗮𝘂𝗹𝘁𝘀: RestTemplate (13.3), WebClient (13.4). Not Java HttpClient. 𝗟𝗲𝗴𝗮𝗰𝘆 𝗰𝗼𝗱𝗲 𝗨𝗻𝗳𝗮𝗺𝗶𝗹𝗶𝗮𝗿𝗶𝘁𝘆 𝗘𝗰𝗼𝘀𝘆𝘀𝘁𝗲𝗺: Fewer Stack Overflow answers, less community content. --- 🔧 𝗝𝗮𝘃𝗮 𝟮𝟭 𝗩𝗶𝗿𝘁𝘂𝗮𝗹 𝗧𝗵𝗿𝗲𝗮𝗱𝘀 𝗖𝗵𝗮𝗻𝗴𝗲 𝗧𝗵𝗲 𝗚𝗮𝗺𝗲 _HttpClient client = HttpClient.newHttpClient(); // Blocking call on virtual thread = no problem String response = client.send(request, BodyHandlers.ofString()).body();_ 𝗕𝗲𝗳𝗼𝗿𝗲 𝘃𝗶𝗿𝘁𝘂𝗮𝗹 𝘁𝗵𝗿𝗲𝗮𝗱𝘀: Blocking HTTP = platform thread per request. Doesn't scale (13.4). 𝗪𝗶𝘁𝗵 𝘃𝗶𝗿𝘁𝘂𝗮𝗹 𝘁𝗵𝗿𝗲𝗮𝗱𝘀: Blocking code performs like async. Simple blocking HttpClient calls scale to thousands of concurrent requests. 𝗧𝗿𝗮𝗱𝗲𝗼𝗳𝗳: WebClient reactive complexity vs HttpClient simplicity + virtual threads. Both scale. Different models. --- 🔧 𝗠𝗲𝗺𝗼𝗿𝘆 & 𝗖𝗼𝗻𝗳𝗶𝗴𝘂𝗿𝗮𝘁𝗶𝗼𝗻 𝗖𝗼𝗻𝗻𝗲𝗰𝘁𝗶𝗼𝗻 𝗽𝗼𝗼𝗹𝗶𝗻𝗴: Built-in, automatic. 𝗛𝗧𝗧𝗣/𝟮 𝘀𝘁𝗮𝘁𝗲: HPACK overhead same as any HTTP/2 client (13.5.2). 𝗗𝗲𝗽𝗲𝗻𝗱𝗲𝗻𝗰𝗶𝗲𝘀: Zero external. 𝗠𝗲𝗺𝗼𝗿𝘆 𝗳𝗼𝗼𝘁𝗽𝗿𝗶𝗻𝘁: fewer jars. --- ⚖️ 𝗧𝗿𝗮𝗱𝗲𝗼𝗳𝗳𝘀 𝗝𝗮𝘃𝗮 𝗛𝘁𝘁𝗽𝗖𝗹𝗶𝗲𝗻𝘁: ✓ HTTP/2 built-in, no dependencies, virtual thread ready ✗ Less Spring integration, smaller ecosystem, less familiar 𝗢𝗸𝗛𝘁𝘁𝗽/𝗔𝗽𝗮𝗰𝗵𝗲: ✓ Mature, large ecosystem, Spring integrated ✗ External dependencies, more complex configuration 𝗪𝗲𝗯𝗖𝗹𝗶𝗲𝗻𝘁: ✓ Reactive, Spring native, high concurrency ✗ Reactor complexity, learning curve (13.4) --- 𝗧𝗵𝗲 𝗕𝗼𝘁𝘁𝗼𝗺 𝗟𝗶𝗻𝗲 Built-in HTTP finally viable. Reconsider dependency bloat. #Java #HttpClient #Java11 #Java21 #VirtualThreads #HTTP2 #Performance #Memory #NoDependencies #JavaPerformance #BackendDevelopment #ProjectLoom #ModernJava #CloudComputing #EnterpriseJava #DevOps #Microservices #MemoryOptimization #SoftwareEngineering #CloudNative #ProductionReady #JavaDevelopment #Async #NonBlocking #HighScale

To view or add a comment, sign in

Explore content categories