在解决这个问题的时候,踩过好多坑;这里只写最终成功的方案,这种方案也是一本万利的写法,不会影响其他的feign接口的调用,最后希望对大家有所帮助
1、首先创建一个配置类
package com.meritdata.dam.common.uploadfile;
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;
public class FeignMultipartSupport {
@Autowired
private ObjectFactory<HttpMessageConverters> messageConverters;
@Bean
public Encoder feignFormEncoder() {
return new SpringFormEncoder(new SpringEncoder(messageConverters));
}
}
注意:这个配置类,不需要加
@Configuration
2、将上面的配置类,配置到@FeignClient注解中
@Service(value = "damDataWarehouseClient")
@FeignClient(value = "dam-datawarehouse", path = "/api/datamanage", configuration = FeignMultipartSupportConfig.class)
public interface DamDataWarehouseClient {
/**
* 导入资源信息
*
* @param file file
*/
@RequestMapping(value = "/resourcecatalog/manage/loadData", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@ResponseBody
ResultBody loadData(@RequestPart("formFile") MultipartFile file) throws Exception;
}