Search code examples
springspring-bootspring-cloudspring-cloud-feign

GET turning into POST with Spring Feign


I was facing an issue that my GET requests were being changed to POST due the RequestHeader and PathVariable that were being interpreted as body of the request in Feign Client.

Interceptor

public class OpenFeignConfiguration implements RequestInterceptor {
    @Value("${key:}")
    private String key;

    @Bean
    Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }

    @Override
    public void apply(RequestTemplate template) {
        template.header("key", key);
    }
}

And the Feign Client

@FeignClient(name = "feignClient", url = "${client.url}", configuration = OpenFeignConfiguration.class)
public interface FeignClient {
    @GetMapping(value = "/path/?test=({var1} and {var2})")
    public Object test(String body, @PathVariable("var1") String var1, @PathVariable("var2") String var2);
}

Solution

  • The solution that I found is that you have to change Springs Feign contract to be Feign one so:

    public class OpenFeignConfiguration implements RequestInterceptor {
       @Value("${key:}")
       private String key;
    
       @Bean
       Logger.Level feignLoggerLevel() {
           return Logger.Level.FULL;
       }
    
       @Bean
       public Contract feignContract() {
           return new Contract.Default();
       }
    
       @Override
       public void apply(RequestTemplate template) {
           template.header("key", key);
       }
    }
    

    And the client now must use the Feign annotation:

    @FeignClient(name = "feignClient", url = "${client.url}", configuration = OpenFeignConfiguration.class)
    public interface FeignClient {
        @RequestLine("GET /path/?test=({var1} and {var2})")
        public Object test(@Param("var1") String originator, @Param("var2") String receiver);
    }
    

    Hope that helps anyone having same issue that I had.