引入依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
服务提供者:
@RestController
@RequestMapping("/file")
public interface FileUploadService {
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST, consumes = MULTIPART_FORM_DATA_VALUE)
CommonResult<String> uploadFile(@RequestPart("file") MultipartFile file,
@RequestParam(value = "containerName", required = false) String containerName
}
具体实现不是重点……根据你的实际情况去完成……
服务调用者:
@RestController
@FeignClient(value = "XXXXXXXX", configuration = FileUploadServiceFeign.ClientConfiguration.class)
@RequestMapping("/file")
public interface FileUploadServiceFeign extends FileUploadService {
/**
* 配置类
*/
class ClientConfiguration {
/**
* 此处注入的是: ObjectFactory<HttpMessageConverters>
*/
@Autowired
private ObjectFactory<HttpMessageConverters> messageConverters;
@Bean
public Encoder feignEncoder() {
return new SpringFormEncoder(new SpringEncoder(messageConverters));
}
}
}
这样就行了……
需要注意的是:
在服务调用者那层的MultipartFile的value要跟服务提供者的@RequestPart中的value值一样。不然它会抛出400异常!!!
成功案例: