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

论坛 期权论坛 脚本     
匿名技术用户   2021-1-7 14:33   67   0

一、错误的方式

例如,我们在子服务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;

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP