如何使用自定义Feign客户端解码JSON响应?

wwodge7n  于 5个月前  发布在  其他
关注(0)|答案(3)|浏览(89)

在我的应用程序中,我必须知道,从列表中的服务器地址,这是向上.我发现的解决方案是从Spring-Boot Actuator调用健康端点,为他们每个人. JSON响应是:

{
    "status": "UP"
}

字符串
在应用程序的其他部分,我使用来自Spring-Cloud的Feign客户端,使用@FeignClient注解定义,它工作得很好:

@FeignClient(
            name = "tokenProxy",
            url = "${host}:${port}"
    )


不幸的是,这种配置不允许重用同一个客户端来调用不同地址上的同一个端点。所以我必须定义自己的自定义客户端(如果有其他解决方案,请不要犹豫告诉我!):

@GetMapping(
      value = "/servers"
    )
    public Server discover() {
      MyClient myClient = Feign.builder()
        .target(
          Target.EmptyTarget.create(
            MyClient.class
          )
        );

      return myClient.internalPing(URI.create("http://localhost:8090"));
    }

    interface MyClient {
        @RequestLine("GET /actuator/health")
        Server internalPing(URI baseUrl);
    }

    class Server {
        private final String status;

        @JsonCreator
        public Server(@JsonProperty("status") String status) {
            this.status = status;
        }

        public String getStatus() {
            return status;
        }
    }


当我调用端点/servers时,我得到以下错误,表明我的自定义Feign客户端与适当的解码器不一致:

feign.codec.DecodeException: class com.xxx.web.Server is not a type supported by this decoder.
    at feign.codec.StringDecoder.decode(StringDecoder.java:34) ~[feign-core-10.10.1.jar:na]
    at feign.codec.Decoder$Default.decode(Decoder.java:92) ~[feign-core-10.10.1.jar:na]
    at feign.AsyncResponseHandler.decode(AsyncResponseHandler.java:115) ~[feign-core-10.10.1.jar:na]
    at feign.AsyncResponseHandler.handleResponse(AsyncResponseHandler.java:87) ~[feign-core-10.10.1.jar:na]
    at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:138) ~[feign-core-10.10.1.jar:na]


我想我应该使用JacksonDecoder,但我在Spring-Cloud Hoxton.SR5的依赖项中找不到它:

<dependencies>
    ...
        <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    ...
      </dependencies>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Hoxton.SR5</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
  </dependencyManagement>


有人可以帮助我,无论是我的需要更好的解决方案或解释如何正确配置自定义Feign客户端?
Thanks in advance

35g0bw71

35g0bw711#

事实上,在使用spring-cloud依赖项时,默认情况下不会加载包含Jackson解码器和编码器的库。为了解决这个问题,我只需将以下内容添加到pom.xml文件中:

<dependency>
  <groupId>io.github.openfeign</groupId>
  <artifactId>feign-jackson</artifactId>
</dependency>

字符串

ohtdti5x

ohtdti5x2#

另一种方法是使用@Import(FeignClientsConfiguration.class)注解类,这是Spring Cloud Netflix提供的默认配置。
然后,在创建Feign客户端时,注入Encoder和Decoder变得很容易:

@Import(FeignClientsConfiguration.class)
    @Configuration
    public class MyConfiguration {
    (...)
    Myclient myClient (Decoder feignDecoder, Encoder feignEncoder) {
    return Feign.builder()
        .decoder( feignDecoder )
        .encoder( feignEncoder )
        .target(
          Target.EmptyTarget.create(
            MyClient.class
          )
        );
    }

字符串
在配置类中有两种不同的定义编码器(可分页的或不可分页的),因此请注意清楚地识别您想要的编码器,无论是通过名称还是限定符。

vzgqcmou

vzgqcmou3#

我不得不在下面添加依赖关系

<dependency>
        <groupId>io.github.openfeign</groupId>
        <artifactId>feign-jackson</artifactId>
        <version>12.0</version> <!-- Use the latest version available -->
    </dependency>

字符串
接下来,我不得不对feign.builder进行调整

MyClient cl = Feign.builder()
            .encoder(new JacksonEncoder())
            .decoder(new JacksonDecoder())
            .target(MyClient.class, urlBaseOrServiceName);

相关问题