使用Spring Feign进入POST

时间:2019-03-29 17:37:29

标签: spring spring-boot spring-cloud spring-cloud-feign

我遇到一个问题,因为在Feign Client中将RequestHeader和PathVariable解释为请求的主体,因此我的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);
    }
}

假冒客户

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

1 个答案:

答案 0 :(得分:0)

我发现的解决方案是您必须将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);
}

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