spring-cloud-feign - Spring 云启动器-openfeign : Invalid HTTP method: PATCH executing PATCH

标签 spring-cloud-feign feign http-patch

语境

我有一个 spring boot (version 2.2.6.RELEASE) web 项目。

从这个 Web 应用程序(我称之为“APP1”)我想使用来自另一个 Web 应用程序的 PATCH 方法调用另一个 URI(我们称之为“APP2”)。
在我的 pom.xml 中,我有以下依赖项:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

下面是我如何调用其他 Web 应用程序的 PATCH 方法。
@FeignClient(name = "clientName", url = "base-uri")
public interface MyInterface{
   @PatchMapping(value = "/target-uri")
    void callClientMethod(Map<String, Object> args);

问题
  • APP2 的 PATCH 方法被有效地调用
  • 但随后 APP1 抛出以下错误:
  • feign.RetryableException:无效的 HTTP 方法:PATCH 正在执行 PATCH

  • 我在互联网上寻找解决方案,并在我的 pom.xml 中添加了以下 snipet
    <dependency>
        <groupId>com.netflix.feign</groupId> <!-- Also tried io.github.openfeign -->
        <artifactId>feign-httpclient</artifactId>
        <version>8.18.0</version>
    </dependency>
    

    之后,APP2 的 PATCH 方法仍然被正确调用,但在 APP1 中我收到以下错误:
    java.lang.NoSuchMethodError: feign.Response.create (ILjava/lang/String;Ljava/util/Map;Lfeign/Response$Body;)Lfeign/Response;


  • 有谁知道如何解决这个错误?

  • 在此先感谢您的帮助 !

    最佳答案

    我遇到了同样的问题,花了很多时间来理解和解决这个问题。
    首先,您需要了解的是,Feign 不支持从框中调用 PATCH http 方法!
    如果您可以更改两个服务中的方法,请使用 PUT 进行更新而不是 PATCH ...

    但是如果您与第三方实现集成,您应该添加一些配置:
    1.添加支持PATCH http方法的依赖:

    // https://mvnrepository.com/artifact/io.github.openfeign/feign-okhttp
    compile group: 'io.github.openfeign', name: 'feign-okhttp', version: '10.2.0'


  • 添加配置:

  • @Configuration 
    public class FeignConfiguration {
        @Bean
        public OkHttpClient client() {
            return new OkHttpClient();
        } 
    }
    

  • 以及带有 Feign 的 PATCH 请求示例:

  • @FeignClient(name = "someapi", url = "${client.someapi.url}")
    @Component
    @RequestMapping("/users")
    public interface SomeClient {
    
        @RequestMapping(value = "/{id}",
                method = RequestMethod.PATCH,
                consumes = MediaType.APPLICATION_JSON_VALUE,
                produces = MediaType.APPLICATION_JSON_VALUE)
        FeignUser update(@PathVariable("id") Long id, @RequestBody Map<String, Object> fields);
    }
    

    希望它可以帮助某人。

    关于spring-cloud-feign - Spring 云启动器-openfeign : Invalid HTTP method: PATCH executing PATCH,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61641977/

    相关文章:

    java - 多个Feign客户端超时配置

    spring - 使用 Swagger 生成 Netflix Feign 代码

    java - 对假客户端的通用响应支持

    Spring Cloud Feign + Sleuth + Zipkin - 需要原始请求

    java - Spring 启动: FeignClient with SSL (p12)

    python - 使用 Colander 验证 PATCH 请求

    java - 以编程方式创建 Feign 客户端和 Eureka 目标

    使用 HTTPClient 的 C# HTTP PATCH

    python - 将 PATCH 与 Django 图像字段和 s3 一起使用

    java - 假装 : file upload configuration