解决Spring Cloud Feign 上传文件报错:the request was rejected because no multipart boundary was found

公司的一个SpringCloud项目 两个模块 A模块调用 ,B模块被调用:

A模块: 有个上传图片去做业务相关操作的接口
B模块: 有个上传图片到服务器的接口

流程就是:选择图片上传,调用A 模块 ,内部A模块再调用B模块

报错信息

the request was rejected because no multipart boundary was found

解决方法:

被调用模块 B模块
1、API代码:

    @PostMapping(value = "/imgAndPath", produces = {MediaType.APPLICATION_JSON_VALUE},
                 consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
    ResultMsg<UpImgResultVo> imgAndPath(@RequestParam(value = "path") String path, @RequestPart("file") MultipartFile file) throws Exception;

2、client 代码:

import com.ezcloud.petbaby.modules.resources.api.UploadApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.cloud.openfeign.FeignClient;
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;


@FeignClient(
        contextId = "pb-resources-client",
        name="${service.pb-resources.name}",
        path="${service.pb-resources.path}",
        configuration = UploadClient.ClientConfiguration.class)
@ConditionalOnMissingClass("com.ezcloud.petbaby.modules.resources.controller.UploadController")
public interface UploadClient extends UploadApi {
    //文件配置转换  (内部类)
    class ClientConfiguration{
        @Autowired
        private ObjectFactory<HttpMessageConverters> messageConverters;

        @Bean
        public Encoder feignEncoder() {
            return new SpringFormEncoder(new SpringEncoder(messageConverters));
        }

    }
}

调用模块代码

@PostMapping(value = "/getTaoBaoByImg",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
ResultMsg<Object> getTaoBaoByImg(@RequestParam("file") MultipartFile file) throws Exception;

​ 至此 Feign 的 form-data 上传成功

只要里面对应接口没问题 就不会有问题

借鉴传送

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值