feign檔案上傳終極解決方案
maven需要增加依賴
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form-spring</artifactId>
<version>3.4.1</version>
</dependency>
配置類
Config.java
.
package com.scen.web.config;
import feign.Logger;
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 注解配置
*
* @author Scen
* @date 2018/4/18 18:31
*/
@Configuration
public class Config {
/**
* 打開feign的日志
*
* @return
*/
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
/**
* 涉及檔案傳輸配置
*/
class MultipartSupportConfig {
@Autowired
private ObjectFactory<HttpMessageConverters> messageConverters;
@Bean
public Encoder feignFormEncoder() {
return new SpringFormEncoder(new SpringEncoder(messageConverters));
}
}
}
feign用戶端需要加的注解
@RequestMapping(value = "/upload", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},
consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
void upload(
@RequestPart("file") MultipartFile file,
) throws Exception;
思路:直接傳沒有任何報錯,但是每次都會進入熔斷器,遂注釋掉熔斷器,發現走不到斷點就報了個Jackson相關錯誤(懷疑是遠端傳輸序列化問題),定位到是encoder這個家夥出現的問題,翻了衆多資料源碼終于解決!同時解決一個feign用戶端同時有上傳檔案方法和其他普通方法調用報錯的問題!
2019-01-08更新:3.4.1版本這種方案正常用,更高的版本試了一下依然報錯,目前沒解決更新版本後又回退了 o(╯□╰)o