title: Adding OkHttp custom interceptor to Feign client
body: <p>I'm having problems to set up a global <code>OkHttp</code> interceptor for my <code>@FeignClient</code> beans. I'm not experiencing any error, but the interceptor is being ignored.</p>
  <p>My understanding is that Spring Cloud's auto configuration should pick the <code>OkHttpClient.Builder</code> bean that I'm declaring and use it to create the underlying <code>OkHttpClient</code> instances, but I might be wrong about this.</p>
  <p>Here are the relevant parts of my Spring app:</p>
  <pre><code>@SpringBootApplication
  @EnableFeignClients(defaultConfiguration = FeignConfig.class)    
  @EnableCircuitBreaker
  public class MyApp {
  
      public static void main(String[] args) {
          SpringApplication.run(MyApp.class);
      }
  }
  </code></pre>
  <hr />
  <pre><code>@Configuration
  public class FeignConfig {
  
      @Bean
      public MyInterceptor myInterceptor() {
          return new MyInterceptor();
      }
  
      @Bean
      public OkHttpClient.Builder okHttpClientBuilder(MyInterceptor interceptor) {
          return new OkHttpClient.Builder().addInterceptor(interceptor);
      }
  }
  </code></pre>
  <hr />
  <pre><code>public class MyInterceptor implements okhttp3.Interceptor {
  
      @Override
      public Response intercept(Chain chain) throws IOException {
  
          Request request = chain.request();
  
          System.out.println(&quot;Hey there, this is my request: &quot; + request);
  
          Response response = chain.proceed(request);
  
          System.out.println(&quot;Hey there, this is my response: &quot; + response);
  
          return response;
      }
  
  }
  </code></pre>
  <p>The <code>intercept</code> method above is never called. I need <code>MyInterceptor</code> to be a Spring bean, because I need to inject other dependencies to it.</p>
  <hr />
  <pre><code>@FeignClient(name = &quot;myClient&quot;, fallback = MyClientFallback.class)
  public interface MyClient {
  
      // method declarations
  }
  </code></pre>
  <hr />
  <pre><code>@Component
  public class MyClientFallback implements MyClient {
  
      // method fallback implementations
  }
  </code></pre>
  <hr />
  <p>Here's the relevant part of my <code>application.properties</code> file:</p>
  <pre><code>feign.hystrix.enabled = true
  feign.okhttp.enabled = true
  
  ribbon.eureka.enabled = false
  ribbon.eager-load.enabled = true
  ribbon.eager-load.clients = myClient
  
  myClient.ribbon.listOfServers = &lt;IP_LIST&gt;
  myClient.ribbon.ServerListRefreshInterval = 10000
  </code></pre>
  <hr />
  <p>As you see from the properties declared above, I'm not using Eureka and I'm using Ribbon to load balance my rest client. I'm also using Hystrix to enable fallback responses and I have set the <code>feign.okhttp.enabled</code> property to <code>true</code>.</p>
  <hr />
  <p>Below is the info about dependecies config and versions...</p>
  <p>Spring Boot version is <code>2.0.3.RELEASE</code> and Spring Cloud version is <code>Finchley.SR1</code>, while <code>OkHttp</code> version is <code>3.11.0</code>.</p>
  <p>In my <code>pom.xml</code> file, I have this <code>spring-cloud-dependencies</code> config:</p>
  <pre><code>&lt;dependencyManagement&gt;
      &lt;dependencies&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
              &lt;artifactId&gt;spring-cloud-dependencies&lt;/artifactId&gt;
              &lt;version&gt;Finchley.SR1&lt;/version&gt;
              &lt;type&gt;pom&lt;/type&gt;
              &lt;scope&gt;import&lt;/scope&gt;
          &lt;/dependency&gt;
  
          ...
  
      &lt;/dependencies&gt;
  &lt;/dependencyManagement&gt;
  </code></pre>
  <p>I have also included the following Spring Boot and Spring Cloud dependencies, along with the <code>OkHttp</code> dependency:</p>
  <pre><code>&lt;parent&gt;
      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
      &lt;version&gt;2.0.3.RELEASE&lt;/version&gt;
  &lt;/parent&gt;
  
  &lt;dependencies&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
          &lt;artifactId&gt;spring-cloud-starter-openfeign&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
          &lt;artifactId&gt;spring-cloud-starter-netflix-hystrix&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
          &lt;artifactId&gt;spring-cloud-starter-netflix-ribbon&lt;/artifactId&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;com.squareup.okhttp3&lt;/groupId&gt;
          &lt;artifactId&gt;okhttp&lt;/artifactId&gt;
          &lt;version&gt;3.11.0&lt;/version&gt;
      &lt;/dependency&gt;
  
      ...
  
  &lt;/dependencies&gt;
  </code></pre>