SpringCloud使用Feign Form实现微服务之间的文件上传
1.pom依赖
<dependency> <groupId>io.github.openfeign.form</groupId> <artifactId>feign-form</artifactId> <version>3.0.1</version> </dependency> <dependency> <groupId>io.github.openfeign.form</groupId> <artifactId>feign-form-spring</artifactId> <version>3.0.1</version> </dependency>
2.MultipartSupportConfig配置类
@Configuration public class MultipartSupportConfig { @Autowired private ObjectFactory<HttpMessageConverters> messageConverters; @Bean public Encoder feignFormEncoder() { return new SpringFormEncoder(new SpringEncoder(messageConverters)); } }
3.后端接口改造
关键字consumes = MULTIPART_FORM_DATA_VALUE
/**
* 文件上传接口
* @param file 上传文件
* @date 2019-11-19 11:56:40
**/
@PostMapping(value = "uploadExamTarget", consumes = MULTIPART_FORM_DATA_VALUE)
public JsonResult uploadExamTarget(@RequestBody MultipartFile file) {
return new JsonResult(coreClient.uploadExamTarget(file));
}
4.FeignClient接口改造
引入配置类MultipartSupportConfig,和接口注解consumes = MULTIPART_FORM_DATA_VALUE
@FeignClient(value = "core-server", configuration = MultipartSupportConfig.class) public interface CoreClient {
/**
* 调用core-server服务的文件上传接口
* @param file 上传文件
* @date 2019-11-19 11:56:40
**/
@RequestMapping(value = "/excel/uploadExamTarget", consumes = MULTIPART_FORM_DATA_VALUE)
String uploadExamTarget(@RequestBody MultipartFile file);
}