How to Add Request and Response Interceptors to OpenFeign with OkHttp in Spring Cloud
This article explains how to replace OpenFeign's default HTTP client with OkHttp, configure request and response interceptors, and customize OkHttpClientBuilder in Spring Cloud, providing detailed code examples and configuration steps for effective interceptor implementation.
Spring Cloud's OpenFeign uses OpenFeign for inter-service calls, which by default relies on javax.net.ssl.HttpsURLConnection that lacks interceptor and connection pool support.
To enable interceptors, replace the default client with a third‑party HTTP client such as HttpClient or OkHttp. The Maven dependencies are:
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
<version>9.7.0</version>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
<version>9.7.0</version>
</dependency>Then disable the default HttpClient and enable OkHttp in application.properties :
feign.httpclient.enabled=false
feign.okhttp.enabled=trueFor request interception, implement feign.RequestInterceptor and register it as a Spring component:
@Component
public class CustomerFeignRequestInterceptor implements RequestInterceptor {
@Override
public void apply(RequestTemplate template) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (attributes != null) {
HttpServletRequest request = attributes.getRequest();
template.header("sessionId", request.getHeader("sessionId"));
}
}
}Response interception can be achieved by customizing the OkHttp client. Define a bean that returns an OkHttpClient.Builder with a response interceptor:
@Configuration
public class FeignOkHttpClientConfig {
@Bean
public OkHttpClient.Builder okHttpClientBuilder() {
return new OkHttpClient.Builder().addInterceptor(new FeignOkHttpClientResponseInterceptor());
}
public static class FeignOkHttpClientResponseInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request originalRequest = chain.request();
Response response = chain.proceed(originalRequest);
MediaType mediaType = response.body().contentType();
String content = response.body().string();
// process content as needed
return response.newBuilder()
.body(ResponseBody.create(mediaType, content))
.build();
}
}
}By customizing the OkHttpClient.Builder , you can add any response interceptor without recreating the entire OkHttpClient , avoiding the fallback to Feign's circuit‑breaker behavior.
In summary, switching to OkHttp, adding a request interceptor, and providing a response interceptor via a custom builder solves the interceptor limitation in Spring Cloud OpenFeign.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
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.