Spring Cloud Feign 使用问题汇总

一、feign 接口设置 @RequestMapping(method = {RequestMethod.POST,RequestMethod=GET})

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.igg.baseorder.client.OrderClient': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: Method queryOrderChargeStat can only contain 1 method field. Found: [GET, POST]

 原因:feign客户端,只支持单种请求方式。

解决:去除一种访求方式。

@GetMapping 或者 @PostMapping

 

二、feign 服务返回的数据应答类型为 text/plain

feign.codec.DecodeException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.igg.framework.common.Result] and content type [text/plain;charset=utf-8]

原因:feign 默认只支持 application/json 和 application/*+json 类型的数据格式。

解决:追加 feign 支持的数据格式。

@Configuration
public class FeignConfig {
    @Bean
    public MyMappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
        return new MyMappingJackson2HttpMessageConverter();
    }
}
public class MyMappingJackson2HttpMessageConverter extends org.springframework.http.converter.json.MappingJackson2HttpMessageConverter {
    public MyMappingJackson2HttpMessageConverter() {
        // 先将原先支持的MediaType列表拷出
        List mediaTypeList = new ArrayList<>(this.getSupportedMediaTypes());
        //加入对text/plain的支持
        mediaTypeList.add(new MediaType("text", "plain", StandardCharsets.UTF_8));
        //将已经加入了text/json的MediaType支持列表设置为其支持的媒体类型列表
        this.setSupportedMediaTypes(mediaTypeList);
    }
}

 

3、调用 feign 服务,启动报错

Field orderClient in com.igg.aggregate.server.service.OrderService required a bean of type 'com.igg.baseorder.client.OrderClient' that could not be found.

原因:feign 服务调用方,没有设置 feign 包的扫描路径。

解决:@EnableFeignClients(basePackages = {"com.igg.baseorder.client"})

 

作者:magic_kid_2010,如果觉得笔者的博客对您有所帮助,欢迎【犒赏】

你可能感兴趣的:(Spring,Cloud,spring,cloud,feign,问题汇总)