title: Override URL and Authentication configuration of Spring Cloud Feign client
body: <p>I am trying to leverage Spring Cloud Feign client for declaring rest endpoints(third-party-endpoints) which will be called based on the request that my controller receives.</p>
  <p>I have declared a feign client interface like:</p>
  <pre><code>    @FeignClient(name = &quot;my-client&quot;, url = &quot;https://abc.xyz.com&quot;, configuration = MyClientConfiguration.class)
      public interface MyFeignClient{
  }
  </code></pre>
  <p><strong>MyClientConfiguration</strong> is an unannotated feign configuration class just exposing <strong>BasicAuthRequestInterceptor</strong> bean preconfigured with some credential.</p>
  <pre><code>public class MyClientConfiguration {
     
      @Bean
      public BasicAuthRequestInterceptor basicAuthRequestInterceptor(){
          return new BasicAuthRequestInterceptor(&quot;USERID&quot;,&quot;PWD&quot;);
      }
  
  }
  </code></pre>
  <p>So, what I have right now is a preconfigured feign client.</p>
  <p>I am not sure how do I deal with the requirement that asks for being able to define/override the feign configuration with the <strong>url</strong> and <strong>credentials</strong> received in the body of different POST request.</p>
  <p>So its not about overriding the feign configuration via properties file(like other somewhat similar SO questions), but instead being able to configure the feign client on the fly based on the <strong>url</strong> and <strong>credentials</strong> received in the body of the incoming requests.</p>
  <p>I am aware of ways to do it with WebClient and looking for something similar to what WebClient provides via its <strong>uri</strong> and <strong>headers</strong> methods. Webclient allows us to override the <strong>webClient</strong> bean configuration with different <strong>uri</strong> and different auth credentials,</p>
  <pre><code>        webClient
                  .put()
                  .uri(&quot;base-uri&quot;, uriBuilder -&gt; uriBuilder.path(&quot;/rest&quot;).path(&quot;/api&quot;).path(&quot;/2&quot;).path(&quot;/issue&quot;).path(&quot;/&quot; + id).build())
                  .headers(httpHeaders -&gt; {
                      httpHeaders.setBasicAuth(&quot;decrypted-username&quot;, &quot;decrypted-pwd&quot;);
                  })
  </code></pre>
  <p>Does Spring Cloud Feign provide any such facility where it is possible to have a generic configuration yet allowing to override those configurations at runtime?</p>
  <p><strong>EDIT 1 : START</strong></p>
  <p>The closest I got to is implementing via the feign's builder pattern :</p>
  <pre><code>    MyFeignClient myFeignClient = Feign.builder()
              .contract(new SpringMvcContract())
              .requestInterceptor(new BasicAuthRequestInterceptor(&quot;decrypted-username&quot;, &quot;decrypted-pwd&quot;)
              .target(MyFeignClient.class, myRequestDTO.getBaseUrl());
  </code></pre>
  <p>I was challenged with an exception because I didn't specify which Contract to use. It was using the Default Contract.I then changed to SpringMvcContract as the annotation that I am using are the Spring MVC ones and not feign's default.</p>
  <p>Getting DecodeException as of now. Trying to fix it. Will post update when done.</p>
  <p><strong>EDIT 1 : END</strong></p>
  <p><strong>EDIT 2 : START</strong></p>
  <p>I was finally able to get this working. I figured out that the <strong>feign-gson</strong> does a great job at deserialization with minimal configuration.
  My final feign configuration with the Decoder in place looks like :</p>
  <pre><code>MyFeignClient myFeignClient = Feign.builder()
                  .contract(new SpringMvcContract())
                  .decoder(new GsonDecoder())
                  .requestInterceptor(new BasicAuthRequestInterceptor(&quot;decrypted-username&quot;, &quot;decrypted-pwd&quot;)
                          .target(MyFeignClient.class, myRequestDTO.getBaseUrl());
  </code></pre>
  <p><strong>EDIT 2 : END</strong></p>