or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

codecs.mdhttp-clients.mdhystrix.mdindex.mdjaxrs.mdjson-processing.mdxml-processing.md
tile.json

http-clients.mddocs/

HTTP Client Implementations

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.

Capabilities

Default HTTP Client

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;
}

Apache HttpClient

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;
}

OkHttp Client

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;
}

Usage Examples

Default HttpURLConnection Client

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");

Apache HttpClient

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");

OkHttp Client

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");

Connection Pooling and Performance

// 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");

SSL and Security Configuration

// 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");

Proxy Configuration

// 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);

Timeout Configuration

// 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();

Installation

Apache HttpClient Module

dependencies {
    compile 'com.netflix.feign:feign-httpclient:8.18.0'
    compile 'org.apache.httpcomponents:httpclient:4.5.13'
}

OkHttp Module

dependencies {
    compile 'com.netflix.feign:feign-okhttp:8.18.0'
    compile 'com.squareup.okhttp:okhttp:2.7.5'
}

Performance Characteristics

Default HttpURLConnection

  • Pros: No additional dependencies, simple configuration
  • Cons: Limited connection pooling, basic features
  • Best for: Simple applications, minimal dependencies

Apache HttpClient

  • Pros: Mature, feature-rich, excellent connection pooling
  • Cons: Larger dependency footprint
  • Best for: Enterprise applications, complex HTTP requirements

OkHttp

  • Pros: Modern, efficient, excellent performance, HTTP/2 support
  • Cons: Additional dependency
  • Best for: High-performance applications, mobile applications

Client Selection Guidelines

Choose your HTTP client based on your requirements:

  1. Default Client: Use for simple applications or when minimizing dependencies
  2. Apache HttpClient: Use for enterprise applications requiring advanced HTTP features
  3. OkHttp: Use for high-performance applications or when HTTP/2 support is needed

Configuration Best Practices

Connection Pooling

  • Set appropriate pool sizes based on expected load
  • Configure keep-alive timeouts to balance resource usage and performance
  • Monitor connection pool metrics

Timeouts

  • Set connect timeouts (5-10 seconds typically)
  • Set read timeouts based on expected response times
  • Use shorter timeouts for health checks

SSL/TLS

  • Use proper certificate validation in production
  • Configure appropriate cipher suites and protocols
  • Consider certificate pinning for mobile applications