【问题标题】:Encode and Decode ResponseEntity when using Feign and Spring MVC?使用 Feign 和 Spring MVC 时对 ResponseEntity 进行编码和解码?
【发布时间】:2018-12-25 07:37:41
【问题描述】:

我正在尝试使用动态伪装。但是在从 RequestMapping 转换响应时我遇到了很多问题。

Controller.java:

@RequestMapping("/users")
public ResponseEntity<List<User>> sendUsers

MyFeignClient.java:

public interface MyFeignClient {

@RequestLine(value="GET /api/users")
ResponseEntity<List<User>> getUsers();}

MainClass.java:

MyFeignClient callService = Feign.builder()
            .encoder(new Encoder.Default())
            .decoder(new Decoder.Default())
            .requestInterceptor(new FeignConfig(props).getJwtRequestInterceptor())
            .target(MyFeignClient.class, "http://localhost:8710");

然后:

ResponseEntity<List<User>> txnPool = callService.getUsers();

但我有以下错误:

feign.codec.DecodeException User 不是此解码器支持的类型

我该如何解决这个问题?

【问题讨论】:

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


    【解决方案1】:

    我使用 JacksonEncoder 和 JacksonDecoder 解决了这个问题

    MyFeignClient callService = Feign.builder()
            .encoder(new JacksonEncoder())
            .decoder(new JacksonDecoder())
            .requestInterceptor(new FeignConfig(props).getJwtRequestInterceptor())
            .target(MyFeignClient.class, "http://localhost:8710");
    

    我还将@Headers("Content-Type: application/json") 添加到我的Feign Interface

    【讨论】:

    • 如果feign客户端接口MyFeignClient被构造函数注入怎么办?此外,使用eureka 注册表,我们不会在localhost:8710 中运行服务器......我们如何在目标文件夹中提及发现客户端名称?如果可能的话,我们如何在配置类中添加这种编码和解码逻辑。
    猜你喜欢
    • 2016-05-16
    • 2015-12-10
    • 2013-12-26
    • 1970-01-01
    • 2020-05-13
    • 1970-01-01
    • 1970-01-01
    • 2017-04-15
    • 1970-01-01
    相关资源
    最近更新 更多