Feign进行文件上传时的各种坑

本文详细记录了使用Feign实现微服务间文件上传的过程,包括解决Content-Type、MultipartFile和编码异常等问题,并提供了完整的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在使用feign调用服务实现文件上传功能时,会遇到各种各样的问题,下面就我在实现过程中遇到的一些问题进行总结。想直接看最终解决办法的可以跳到4。

1.使用Post请求时我习惯将所有数据装在一个map里,在接收端在逐个读取出来。

Result upload(@RequestBody Map map)

但是在这里使用这个方法会报错无法读取。换种方法,将每个数据单独发送:

Result upload(@RequestBody("file") MultipartFile file, @RequestParam("assessId") Integer assessId);

其中第一个数据用RequestBody参考了这篇文章:Feign消费服务时POST/GET请求方式

2.在更换后,传到服务端的file为空,查到数据发现需要将 @RequestBody 改为 @RequestPart,参考文章通过Feign服务实现文件上传

3.随后调试发现又报错:

Current request is not a multipart request

经查阅资料发现需要设置请求content-type为【multipart/form-data】,参考文章Feign传输Multipartfile文件的正确方式,Current request is not a multipart request报错解决

4.随后的结果是:还在报错!

feign.codec.EncodeException: Could not write request: no suitable HttpMessageConverter found for request type [org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile] and content type [multipart/form-data]

经过各种搜索资料,试了各种方法,终于用下面的方法试通了。
①增加依赖

    <dependency>
      <groupId>io.github.openfeign.form</groupId>
      <artifactId>feign-form</artifactId>
      <version>3.3.0</version>
    </dependency>
    <dependency>
      <groupId>io.github.openfeign.form</groupId>
      <artifactId>feign-form-spring</artifactId>
      <version>3.3.0</version>
    </dependency>
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.3</version>
    </dependency>

②增加配置类

@Configuration
public class FeignSupportConfig {

    @Autowired
    private ObjectFactory<HttpMessageConverters> messageConverters;

   @Bean
    public SpringFormEncoder feignFormEncoder(){
        return new SpringFormEncoder();
    }
}

③远程调用接口

@Component
@FeignClient(name = "assessment", configuration = FeignSupportConfig.class)
public interface AssessUploadInterface {

    @PostMapping(value = "/assessment/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    Result uploadKingData(@RequestPart("file") MultipartFile file, @RequestParam("assessId") Integer assessId) throws Exception;
}

④服务接收

@ResponseBody
@PostMapping(value = "/assessment/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public Result uploadKingData(@RequestPart("file") MultipartFile file, @RequestParam("assessId") Integer assessId) throws IOException {
}

Feign实现微服务间文件上传(Finchley版本)
完结,撒花

2020-07-29补充:
经过上面的修改后虽然文件可以上传了,但是,部分post的请求在调用服务提供者时直接报错!get请求是正常的!
报错如下

class java.util.HashMap is not a type supported by this encoder.

经过查阅资料发现FormEncoderr的encode方法当传送的对象是json格式的字符串的时候,就会调用 _flddelegate.encode,即Encoder.Default的encode方法,而这个Encoder.Default的encode方法判断传送的类型不是String或者byte[],就会抛异常。需要对之前新增的配置文件进行修改。即将配置类

@Configuration
public class FeignSupportConfig {

    @Autowired
    private ObjectFactory<HttpMessageConverters> messageConverters;

   @Bean
    public SpringFormEncoder feignFormEncoder(){
        return new SpringFormEncoder();
    }
}

修改为

@Configuration
public class FeignSupportConfig {

    @Autowired
    private ObjectFactory<HttpMessageConverters> messageConverters;

//    @Bean
//    public SpringFormEncoder feignFormEncoder(){
//        return new SpringFormEncoder();
//    }

    @Bean
    public SpringFormEncoder feignFormEncoder(){

        return new SpringFormEncoder(new SpringEncoder(messageConverters));
    }
}

ok,完美解决。参考:spring cloud feign 上传文件报not a type supported by this encoder解决方案

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值