Feign provides pluggable HTTP client implementations, allowing you to choose the best HTTP client for your needs, from the default HttpURLConnection to high-performance alternatives like OkHttp and Apache HttpClient.
Built-in HTTP client using Java's HttpURLConnection.
/**
* Default HTTP client implementation using HttpURLConnection
*/
public static class Client.Default implements Client {
/** Create default client with no SSL configuration */
public Default(SSLSocketFactory sslContextFactory, HostnameVerifier hostnameVerifier);
/** Execute HTTP request */
public Response execute(Request request, Options options) throws IOException;
}High-performance HTTP client using Apache HttpComponents.
/**
* HTTP client implementation using Apache HttpClient
*/
public final class ApacheHttpClient implements Client {
/** Create client with default HttpClient */
public ApacheHttpClient();
/** Create client with custom HttpClient */
public ApacheHttpClient(HttpClient client);
/** Execute HTTP request */
public Response execute(Request request, Options options) throws IOException;
}Modern HTTP client using Square's OkHttp library.
/**
* HTTP client implementation using OkHttp
*/
public final class OkHttpClient implements Client {
/** Create client with default OkHttpClient */
public OkHttpClient();
/** Create client with custom OkHttpClient */
public OkHttpClient(com.squareup.okhttp.OkHttpClient client);
/** Execute HTTP request */
public Response execute(Request request, Options options) throws IOException;
}import feign.Feign;
import feign.Client;
// Default client (HttpURLConnection)
GitHub github = Feign.builder()
.client(new Client.Default(null, null))
.target(GitHub.class, "https://api.github.com");
// With SSL configuration
SSLContext sslContext = SSLContext.getDefault();
GitHub github = Feign.builder()
.client(new Client.Default(sslContext.getSocketFactory(), null))
.target(GitHub.class, "https://api.github.com");import feign.Feign;
import feign.httpclient.ApacheHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.CloseableHttpClient;
// Basic Apache HttpClient
GitHub github = Feign.builder()
.client(new ApacheHttpClient())
.target(GitHub.class, "https://api.github.com");
// Custom Apache HttpClient configuration
CloseableHttpClient httpClient = HttpClients.custom()
.setMaxConnTotal(100)
.setMaxConnPerRoute(20)
.build();
GitHub github = Feign.builder()
.client(new ApacheHttpClient(httpClient))
.target(GitHub.class, "https://api.github.com");import feign.Feign;
import feign.okhttp.OkHttpClient;
import com.squareup.okhttp.ConnectionPool;
import java.util.concurrent.TimeUnit;
// Basic OkHttp client
GitHub github = Feign.builder()
.client(new OkHttpClient())
.target(GitHub.class, "https://api.github.com");
// Custom OkHttp configuration
com.squareup.okhttp.OkHttpClient okClient = new com.squareup.okhttp.OkHttpClient();
okClient.setConnectTimeout(10, TimeUnit.SECONDS);
okClient.setReadTimeout(30, TimeUnit.SECONDS);
okClient.setConnectionPool(new ConnectionPool(50, 5 * 60 * 1000));
GitHub github = Feign.builder()
.client(new OkHttpClient(okClient))
.target(GitHub.class, "https://api.github.com");// Apache HttpClient with connection pooling
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setMaxTotal(100);
connectionManager.setDefaultMaxPerRoute(20);
CloseableHttpClient httpClient = HttpClients.custom()
.setConnectionManager(connectionManager)
.setRetryHandler(new DefaultHttpRequestRetryHandler(3, true))
.build();
API api = Feign.builder()
.client(new ApacheHttpClient(httpClient))
.target(API.class, "https://api.example.com");
// OkHttp with connection pooling
com.squareup.okhttp.OkHttpClient okClient = new com.squareup.okhttp.OkHttpClient();
okClient.setConnectionPool(new ConnectionPool(100, 5 * 60 * 1000)); // 100 connections, 5 min keep-alive
API api = Feign.builder()
.client(new OkHttpClient(okClient))
.target(API.class, "https://api.example.com");// Apache HttpClient with SSL
SSLContext sslContext = SSLContexts.custom()
.loadTrustMaterial(trustStore, new TrustSelfSignedStrategy())
.build();
SSLConnectionSocketFactory sslFactory = new SSLConnectionSocketFactory(
sslContext,
NoopHostnameVerifier.INSTANCE
);
CloseableHttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(sslFactory)
.build();
API api = Feign.builder()
.client(new ApacheHttpClient(httpClient))
.target(API.class, "https://api.example.com");
// OkHttp with SSL
com.squareup.okhttp.OkHttpClient okClient = new com.squareup.okhttp.OkHttpClient();
okClient.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true; // Custom hostname verification
}
});
API api = Feign.builder()
.client(new OkHttpClient(okClient))
.target(API.class, "https://api.example.com");// Apache HttpClient with proxy
HttpHost proxy = new HttpHost("proxy.example.com", 8080);
RequestConfig config = RequestConfig.custom()
.setProxy(proxy)
.build();
CloseableHttpClient httpClient = HttpClients.custom()
.setDefaultRequestConfig(config)
.build();
// OkHttp with proxy
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.example.com", 8080));
com.squareup.okhttp.OkHttpClient okClient = new com.squareup.okhttp.OkHttpClient();
okClient.setProxy(proxy);// Configure timeouts through Request.Options
GitHub github = Feign.builder()
.client(new ApacheHttpClient())
.options(new Request.Options(5000, 30000)) // 5s connect, 30s read
.target(GitHub.class, "https://api.github.com");
// Client-specific timeout configuration
CloseableHttpClient httpClient = HttpClients.custom()
.setDefaultRequestConfig(RequestConfig.custom()
.setConnectTimeout(5000)
.setSocketTimeout(30000)
.build())
.build();dependencies {
compile 'com.netflix.feign:feign-httpclient:8.18.0'
compile 'org.apache.httpcomponents:httpclient:4.5.13'
}dependencies {
compile 'com.netflix.feign:feign-okhttp:8.18.0'
compile 'com.squareup.okhttp:okhttp:2.7.5'
}Choose your HTTP client based on your requirements: