SpringCloud组件feign传递MultipartFile

前言

SpringCloud 组件fiegn默认是不支持传递文件的. 但是github上有人写出了解决方法.

方法
  1. 引入POM
        
        
            org.springframework.cloud
            spring-cloud-starter-feign
             provided
        
        
            io.github.openfeign.form
            feign-form
            3.3.0
        
        
            io.github.openfeign.form
            feign-form-spring
            3.3.0
        
  1. 编写配置类
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;

import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;

@Configuration
public class FeignMultipartSupportConfig {

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

    @Bean
    public feign.Logger.Level multipartLoggerLevel() {
        return feign.Logger.Level.FULL;
    }
}
  1. 使用
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

//common是eureka中的一个服务提供者 
@FeignClient(name="common", configuration=FeignMultipartSupportConfig.class)
public interface FeignUploadService {
    @RequestMapping(value="uploadImg", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public OSSFile uploadImg(@RequestPart MultipartFile file);
}

你可能感兴趣的:(SpringCloud组件feign传递MultipartFile)