SpringCloud 单文件上传 服务间调用

项目中有文件上传的功能开发,记录一次问题解决,话不多说:

增加 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方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值