phenomenon
Spring Cloud Feign component not support file upload between micron service.
it may throw exception like this:
1 | feign.codec.EncodeException: class [Lorg.springframework.web.multipart.MultipartFile; is not a type supported by this encoder. |
add dependencies
1 | <dependency> |
https://mvnrepository.com/artifact/io.github.openfeign.form/feign-form-spring
be careful the feign-form-spring depend on spring-web it may have conflict with your project.
for example in spring-boot:2.0.5.RELEASE spring-cloud:Finchley.SR1 project has already importspring-web:5.0.9.RELEASE, if import feign-form-spring:3.3.0 it will import spring-web:4.3.18.RELEASE too,
it cause spring bean creation exception.
1 | <dependency> |
file receive part
1 | ("file upload receive part") |
be careful consumes property must add to the annotation RequestMappingmethod property must be POST, RequestPart is working.
feign client part
client part is a little complex.
add configuration class:
1 |
|
SpringFormEncodersupport single file upload.FeignSpringFormEncodersupport file array upload.SpringEncoderavoid project makeRequestBodyencoding error.
add form encoder
1 | public class FeignSpringFormEncoder extends FormEncoder { |
add interface:
1 | (name = "upload-example", configuration = MultipartSupportConfig.class) |
in annotation FeignClient add configuration property, point to the encoding support class.
Testing
1 | 4j |
https://github.com/OpenFeign/feign-form
https://www.jianshu.com/p/4f4d9d084b1d
http://blog.didispace.com/spring-cloud-starter-dalston-2-4/
https://blog.csdn.net/gududedabai/article/details/79895893