Feign passes MultipartFile problem

Project scenario:

Service B wants to upload files to Service A through Feign

Problem Description:

FeignClient is used here to upload files through MultipartFile, and the following error is reported:

 class XXX is not a type supported by this encoder.

Cause Analysis:

OpenFeign 默认不支持文件参数 MultipartFile 。

solution:

1. For parameters of type MultipartFile on the server side, use the annotation @RequestPart(“file”)

@PostMapping("/uploadMultipleFile")
    public Result uploadImg(@RequestPart("file") MultipartFile[] files){
        try {
            return uploadService.uploadImg(files);
        }catch (Exception e){
            log.info("文件上传异常"+ ExceptionUtils.getStackTrace(e));
            return new Result(ResultCode.FAIL,"文件上传异常");
        }
    }

2. Add the following coordinates to the calling end

 <!--feign-form-->
        <dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form</artifactId>
            <version>3.8.0</version>
        </dependency>
        <!--配合spring使用-->
        <dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form-spring</artifactId>
            <version>3.8.0</version>
        </dependency>

  <!--openfeign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

3. Add configuration class

import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;


@Configuration
public class MultipartSupportConfig {

    @Bean
    @Primary
    @Scope("prototype")
    public Encoder multipartFormEncoder() {
        return new SpringFormEncoder();
    }

    @Bean
    public feign.Logger.Level multipartLoggerLevel() {
        return feign.Logger.Level.FULL;
    }

}

4. Calling end feign interface

import com.qianxian.common.util.Result;
import com.qianxian.user.config.MultipartSupportConfig;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 上传接口
 * @author yyj
 */
@FeignClient(name="ldd-upload",configuration =  MultipartSupportConfig.class)
public interface UploadControllerFeign {

    /**
     * 图片上传
     * @param files
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/uploadMultipleFile",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    Result uploadImg(@RequestPart("file") MultipartFile[] files);
}

5.Controller layer call

 @PostMapping("/uploadMultipleFile")
    public Result uploadImg(@RequestPart("file") MultipartFile[] files){
        try {
            return uploadControllerFeign.uploadImg(files);
        }catch (Exception e){
            log.info("文件上传异常"+ ExceptionUtils.getStackTrace(e));
            return new Result(ResultCode.FAIL,"文件上传异常");
        }
    }

Supongo que te gusta

Origin blog.csdn.net/qq_16504067/article/details/131419716
Recomendado
Clasificación