spring-cloud-starter-openfeign:无效的 HTTP 方法:PATCH 执行 PATCH

2023-11-25

Context

我有一个 Spring Boot(版本 2.2.6.RELEASE)Web 项目。

从这个 Web 应用程序(我称之为“APP1”)中,我想使用另一个 Web 应用程序(我们称之为“APP2”)的 PATCH 方法调用另一个 URI。 在我的 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);

Problem

  • APP2 的 PATCH 方法被有效调用
  • But then APP1 throws the following error:
    • feign.RetryableException:无效的 HTTP 方法:PATCH 执行 PATCH

我在互联网上寻找解决方案,并将以下代码片段添加到我的 pom.xml 中

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

Question

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

在此先感谢您的帮助 !



I had the same problem and spent a lot of time for understand and resolve this problem.
First what you need to understand that is the Feign doesn't support PATCH http method for call from the box!
And if you can change methods in both services use PUT for update instead PATCH...

But if you integrate with third party implementation you should add some configurations:
1. Add dependency which support PATCH http method:

// https://mvnrepository.com/artifact/io.github.openfeign/feign-okhttp
编译组:'io.github.openfeign',名称:'feign-okhttp',版本: ‘10.2.0’

  1. 添加配置:
@Configuration 
public class FeignConfiguration {
    @Bean
    public OkHttpClient client() {
        return new OkHttpClient();
    } 
}
  1. 使用 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);
}

希望它能帮助某人。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

spring-cloud-starter-openfeign:无效的 HTTP 方法:PATCH 执行 PATCH 的相关文章

随机推荐