【问题标题】:How to call api correct with Feign?如何用 Feign 正确调用 api?
【发布时间】:2020-05-24 15:57:24
【问题描述】:

我有调用 TMDB api 的应用程序。对于 api 调用,我使用 Feign 接口:

@FeignClient(name = "TMDb-movie", url = "${TMDB_URL}", path = "/movie")
public interface TmdbMovieRMI {

    @GetMapping(value = "/{id}", consumes = MediaType.APPLICATION_JSON_VALUE)
    ResponseEntity<String> findById(@PathVariable Integer id,
                                    @RequestParam("api_key") String apiKey);
}

但是当我提出请求时,我遇到了这个错误:feign.codec.DecodeException: No qualifying bean of type 'org.springframework.boot.autoconfigure.http.HttpMessageConverters' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} 如何解决?

【问题讨论】:

    标签: java spring-cloud-feign feign


    【解决方案1】:

    如果您不使用 webmvc(例如,因为您使用的是 webflux 或根本没有 web spring boot starter)feign 客户端将无法开箱即用。

    您必须手动定义编码器和解码器。将此插入 @Configuration 类或 @SpringBootApplication 类中:

    @Bean
    public Decoder decoder(ObjectMapper objectMapper) {
        return new JacksonDecoder(objectMapper);
    }
    
    @Bean
    public Encoder encoder(ObjectMapper objectMapper) {
        return new JacksonEncoder(objectMapper);
    }
    

    也许你的pom.xml 中还需要这个额外的依赖:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-08
      • 2020-11-29
      • 1970-01-01
      • 1970-01-01
      • 2020-08-16
      • 2021-01-10
      • 1970-01-01
      相关资源
      最近更新 更多