项目中有文件上传的功能开发,记录一次问题解决,话不多说:
增加 maven (版本我这里用的是3.5.0的)
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form-spring</artifactId>
<version>3.5.0</version>
</dependency>
fegin-client
@FeignClient(name = "xxx",path = "xxx",fallback = xxxFallback.class, configuration = FeignMultipartSupportConfig.class) public interface ITTrustQuarterService{ @RequestMapping(value = "/selectById/{id}", method= RequestMethod.GET) ResultVO selectById(@PathVariable("id") String id); @RequestMapping(value = "/uploadExcel", method= RequestMethod.POST,produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},consumes = MediaType.MULTIPART_FORM_DATA_VALUE) ResultVO uploadExcel(@RequestPart(value = "file") MultipartFile file,@RequestParam(value = "loginId") Long loginId, @RequestParam(value = "loginName") String loginName); }
查询资料,所以有了上面的代码,并进行配置了: FeignMultipartSupportConfig配置文件,如下:fegin-config
@Configuration public class FeignMultipartSupportConfig { @Bean @Primary @Scope("prototype") public Encoder multipartFormEncoder() { return new SpringFormEncoder(); // 这里使用了spring自带的表单解码 } @Bean public feign.Logger.Level multipartLoggerLevel() { return feign.Logger.Level.FULL; } }
到这里,文件就能正常上传了, 但是其他表单提交发生了报错:
Caused by: feign.codec.EncodeException: class com.xjj.information.model.TBankacctRate is not a type supported by this encoder.
at feign.codec.Encoder$Default.encode(Encoder.java:94) ~[feign-core-10.1.0.jar:na]
at feign.form.FormEncoder.encode(FormEncoder.java:90) ~[feign-form-3.5.0.jar:na]
at feign.form.spring.SpringFormEncoder.encode(SpringFormEncoder.java:64) ~[feign-form-spring-3.5.0.jar:na]
at feign.ReflectiveFeign$BuildEncodedTemplateFromArgs.resolve(ReflectiveFeign.java:372) ~[feign-core-10.1.0.jar:na]
at feign.ReflectiveFeign$BuildTemplateByResolvingArgs.create(ReflectiveFeign.java:224) ~[feign-core-10.1.0.jar:na]
at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:74) ~[feign-core-10.1.0.jar:na]
at feign.hystrix.HystrixInvocationHandler$1.run(HystrixInvocationHandler.java:106) ~[feign-hystrix-10.1.0.jar:na]
at com.netflix.hystrix.HystrixCommand$2.call(HystrixCommand.java:302) ~[hystrix-core-1.5.18.jar:1.5.18]
at com.netflix.hystrix.HystrixCommand$2.call(HystrixCommand.java:298) ~[hystrix-core-1.5.18.jar:1.5.18]
at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:46) ~[rxjava-1.2.0.jar:1.2.0]
... 154 common frames omitted
原因是: 非文件上传类型的(MultipartFile),会调用Default.encode解码: 传送的类型不是String或者byte[],就会抛异常
查询资料:解决如下,修改fegin-config文件中的表单转码方式:
@Bean @Primary @Scope("prototype") public Encoder multipartFormEncoder() { return new SpringFormEncoder(new SpringEncoder(new ObjectFactory<HttpMessageConverters>() { @Override public HttpMessageConverters getObject() throws BeansException { return new HttpMessageConverters(new RestTemplate().getMessageConverters()); } })); }
如上修改后,就可以正常了,可是是什么原因呢?
此时变不走default的encode方法