How to Customize HTTP Clients for LangChain4j LLM Integration in Java
This guide explains how LangChain4j modules let you replace the default HTTP client used to call LLM provider APIs, showing two out‑of‑the‑box implementations (JdkHttpClient and SpringRestClient) and providing step‑by‑step code examples for custom JDK and Spring RestClient configurations.
0 Introduction
Some LangChain4j modules (currently OpenAI and Ollama) allow you to provide a custom HTTP client for calling LLM provider APIs.
The langchain4j-http-client module defines an HttpClient SPI that these modules use to invoke the provider's REST API. By implementing this SPI you can integrate any HTTP client.
1 Implementation Options
There are two ready‑made implementations:
1.1 JdkHttpClient
The langchain4j-http-client-jdk module contains JdkHttpClient. When you use a supported module such as langchain4j-open-ai, this client is used by default.
1.2 SpringRestClient
The langchain4j-http-client-spring-restclient module provides SpringRestClient. If you use a Spring Boot starter like langchain4j-open-ai-spring-boot-starter, this client becomes the default.
2 Customizing the JDK HttpClient
HttpClient.Builder httpClientBuilder = HttpClient.newBuilder()
.sslContext(...);
JdkHttpClientBuilder jdkHttpClientBuilder = JdkHttpClient.builder()
.httpClientBuilder(httpClientBuilder);
OpenAiChatModel model = OpenAiChatModel.builder()
.httpClientBuilder(jdkHttpClientBuilder)
.apiKey(System.getenv("OPENAI_API_KEY"))
.modelName("gpt-4o-mini")
.build();3 Customizing Spring's RestClient
RestClient.Builder restClientBuilder = RestClient.builder()
.requestFactory(new HttpComponentsClientHttpRequestFactory());
SpringRestClientBuilder springRestClientBuilder = SpringRestClient.builder()
.restClientBuilder(restClientBuilder)
.streamingRequestExecutor(new VirtualThreadTaskExecutor());
OpenAiChatModel model = OpenAiChatModel.builder()
.httpClientBuilder(springRestClientBuilder)
.apiKey(System.getenv("OPENAI_API_KEY"))
.modelName("gpt-4o-mini")
.build();The article is part of the Java-Interview-Tutorial GitHub repository, which collects these technical notes.
JavaEdge
First‑line development experience at multiple leading tech firms; now a software architect at a Shanghai state‑owned enterprise and founder of Programming Yanxuan. Nearly 300k followers online; expertise in distributed system design, AIGC application development, and quantitative finance investing.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
