springcloud feign调用 文件上传 使用 openfeign 和cloud的版本号问题 feign/rqeustBody 异常

springcloud 版本号 2.x

<version>2.0.9.RELEASE</version>
<spring-cloud.version>Finchley.SR2</spring-cloud.version>

对应的openfeign版本号 :(注意使用3.0.8版本号或者其他的都会报错 解决方案很多都是吧http-core 升级到10版本, 根本不用, 换成下面的3.0.3版本即可)

<dependency>
    <groupId>io.github.openfeign.form</groupId>
    <artifactId>feign-form</artifactId>
    <version>3.0.3</version>
</dependency>

<dependency>
    <groupId>io.github.openfeign.form</groupId>
    <artifactId>feign-form-spring</artifactId>
    <version>3.0.3</version>
</dependency>

服务提供者:

@PostMapping(value = "/buyer/upload/uploadImgFeign",produces = "application/json;charset=UTF-8",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
Res<String> uploadImgFeign(@RequestParam("code") Long code, @RequestPart("file") MultipartFile file) {
    System.out.println("code = " + code);
    return null;
}

feign的写法

@FeignClient(value = "img-service",  configuration = ImgClient.ClientConfiguration.class)
public interface BaseImgClient {

    /**
     * 判断平台是否支持商家功能
     * //不需要登录consumes 必须
     */
    @RequestMapping(value = "/upload,method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    RespData uploadImgFeign(@RequestParam("code") Long code, @RequestPart("file") MultipartFile file);
    /**
     * 配置类
     */
    class ClientConfiguration {
        /**
         * 此处注入的是: ObjectFactory<HttpMessageConverters>
         */
        @Autowired
        private ObjectFactory<HttpMessageConverters> messageConverters;
        @Bean
        public Encoder feignEncoder() {
            return new SpringFormEncoder(new SpringEncoder(messageConverters));
        }
    }

}

服务调用者

@PostMapping(value = "/upload", produces = "application/json; charset=utf-8")
@ResponseBody
public Res<String> upload(@RequestParam("code") Long code, @RequestPart("file")MultipartFile file) {
    imgClient.uploadImgFeign(code,file);
    return null;
}

事实上: feign 和 服务调用者 的注解使用 requestBody也可以程功 唯独使用其他作者说的 使用requestParam不行.

===========================我是版本分割线啦拉============================================

 

springcloud 1.X  我搞了又搞, 改了又改, 实在不行啊啊啊啊啊啊啊

 

待续....

朋友最后用的是 httpclient 请求过去的 , 其实就是 自己本地A请求自己本地B, 1.0我也不知道怎么弄了, 工期实在是忙

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值