spring cloud feign 实现文件传输
工作中遇到需要用feign实现图片传输的需求,网上查找资料之后,发现定义FeignEncoderConfig的实现方式
@Configuration
public class FeignEncoderConfig {
@Bean
public Encoder feignFormEncoder() {
return new SpringFormEncoder();
}
}
尝试过后发现,可以实现图片传输了,但是别的接口会报.class is not a type supported by this encoder.的错误。一番尝试之后,所有接口都通了,完整实现如下:
feign 被调用方
@RestController
@RequestMapping("/feign")
public class FeignProvider {
@RequestMapping(value = "/photo", method = RequestMethod.POST,consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public String takePhoto(@RequestPart("file") MultipartFile file){
return file.getName();
}
}
feign调用方
@Service
@FeignClient(value = "photo-example")
public interface FeignService {
@RequestMapping(value = "/photo", method = RequestMethod.POST,consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
String takePhoto(@RequestPart("file") MultipartFile file);
}
测试之后成功调用