Customizing HttpClient
The classic REST client exposes the settings of the underlying Apache HttpClient through fluent builder methods and overridable factory methods.
When to use it
Use this when the defaults are not enough, for example to change redirect handling, supply your own connection manager, or add request interceptors.
These methods belong to the classic client (juneau-rest-client-classic), which is built on Apache HttpClient 4.5. The
recommended juneau-rest-client configures its HTTP library through a pluggable
transport instead.
Example
All methods on the HttpClientBuilder class have been extended with fluent setters on RestClient.Builder.
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.juneau.rest.client.classic.*;
public class CustomizedClientExample {
public static void main(String[] args) throws Exception {
HttpRequestInterceptor interceptor = (request, context) -> request.addHeader("X-Trace", "demo");
// Create a client with customized HttpClient settings.
try (RestClient client = RestClient
.create()
.json()
.disableRedirectHandling()
.connectionManager(new PoolingHttpClientConnectionManager())
.addInterceptorFirst(interceptor)
.build()) {
System.out.println("Built " + client.getClass().getSimpleName());
}
}
}
Options
Several methods are provided for customizing the underlying HTTP client and client builder classes:
See also
- Extending RestClient
- HTTP Transport Adapters
- HttpClientBuilder in the Apache HttpClient docs