Feign传输Multipartfile文件的正确方式,Current request is not a multipart request报错解决

Feign传输Multipartfile文件的正确方式,Current request is not a multipart request报错解决

一、错误的方式

例如,我们在子服务A的controller中,有一个接收Multipartfile文件的POST请求接口,通常写成如下方式

   @PostMapping("/upload")
    public String upload(
            @RequestParam("pic") MultipartFile pic,
            @RequestParam("otherparam") String otherParam
    ) throws Exception{
        //.....
        
        return "success";
    }

现在子服务B需要调用A服务上面的接口,在feign中发送file时,很多人习惯写成下面样子

    @PostMapping("/service_a/upload")
    String upload(
            @RequestParam("pic") MultipartFile pic,
            @RequestParam("otherparam") String otherParam
    ) throws Exception;

发送请求后,在服务A的控制台,可以看到报错日志如下

org.springframework.web.multipart.MultipartException:Current request is not a multipart request.......

二、正确的方式

由上可知,报错提示当前请求不是一个multipart request

原因是在feign中,发送 multipartfile文件,应该使用【@RequestPart】而不是【@RequestParam】,且需要设置请求content-type为【multipart/form-data】,所以正确写法如下

    //正确方式
    @PostMapping(value = "/service_a/upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    String upload2(
            @RequestPart("pic") MultipartFile pic,
            @RequestParam("otherparam") String otherParam
    ) throws Exception;
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值