GET 变成 POST with Spring Feign

GET turning into POST with Spring Feign

由于 RequestHeader 和 PathVariable 在 Feign Client 中被解释为请求正文,因此我的 GET 请求被更改为 POST。

拦截器

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

和 Feign 客户端

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

我找到的解决方案是,您必须将 Springs Feign 合同更改为 Feign 合同:

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

并且客户端现在必须使用 Feign 注释:

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

希望能帮助遇到与我相同问题的任何人。