使用Spring Cloud Feign上传文件的示例

网友投稿 482 2023-03-05


使用Spring Cloud Feign上传文件的示例

最近经常有人问Spring Cloud Feign如何上传文件。有团队的新成员,也有其他公司的兄弟。本文简单做个总结——

早期的Spring Cloud中,Feign本身是没有上传文件的能力的(1年之前),要想实现这一点,需要自己去编写Encoder 去实现上传。现在我们幸福了很多。因为Feign官方提供了子项目feign-form ,其中实现了上传所需的 Encoder 。

注:笔者测试的版本是Edgware.RELEASE。Camden、Dalston同样适应本文所述。

加依赖

io.github.openfeign.form

feign-form

3.0.3

io.github.openfeign.form

feign-form-spring

3.0.3

编写Feign Client

@FeignClient(name = "ms-content-sample", configuration = UploadFeignClient.MultipartSupportConfig.class)

public interface UploadFeignClient {

@RequestMapping(value = "/upload", method = RequestMethod.POST,

produces = {MediaType.APPLICATION_jsON_UTF8_VALUE},

consumes = MediaType.MULTIPART_FORM_DATA_VALUE)

@ResponseBody

String handleFileUpload(@RequestPart(value = "file") MultipartFile file);

class MultipartSupportConfig {

@Bean

public Encoder feignFormEncoder() {

return new SpringFormEncoder();

}

}

}

如代码所示,在这个Feign Client中,我们引用了配置类MultipartSupportConfig ,在MultipartSupportConfig 中,我们实例化了SpringFormEncoder 。这样这个Feign Client就能够上传啦。

注意点

@RequestMapping(value = "/upload", method = RequestMethod.POST,

produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},

consumes = MediaType.MULTIPART_FORM_DATA_VALUE)

中的produeces 、consumes 不能少;

接口定义中的注解@RequestPart(value = "file") 不能写成@RequestParam(value = "file" 。

最好将Hystrix的超时时间设长一点,例如5秒,否则可能文件还没上传完,Hystrix就超时了,从而导致客户端侧的报错。

SpringCloud中使用Feign的坑

示例如下:

@FeignClient("service-resource")

//@RequestMapping("/api/test")

public interface TestResourceItg {

@RequestMapping(value = "/api/test/raw", method = RequestMethod.POST, consumes = "application/x-www-form-urlencoded")

public String raw1(@PathVariable("subject") String subject, // 标题

@RequestParam("content") String content); // 内容

}

说明:

*使用RequestMapping中的consumes指定生成的请求的Content-Type

*RequestParam指定的参数会拼接在URL之后,如: ?name=xxx&age=18

*PathVariable指定的参数会放到一个Linked

http://

HashMap传入到fe

ign的Encoder中进行处理,而在Spring中实现了该接口的Encoder为SpringEncoder,而该实现又会使用Spring中的HttpMessag

eConverter进行请求体的写入。

坑:

*不要在接口类名上使用RequestMapping,虽然可以使用,但同时SpringMVC会把该接口的实例当作Controller开放出去,这个可以在启动的Mapping日志中查看到

*使用默认的SpringEncoder,在不指定consumes时,PathVariable中的参数会生成JSON字符串发送,且默认情况下不支持Form表单的生成方式,原因为:FormHttpMessageConverter只能处理MultiValueMap,而使用PathVariable参数被放在了HashMap中。默认更不支持文件上传。其实已经有支持处理各种情况的HttpMessageConverter存在。

填坑:

*支持Form表单提交:只需要编写一个支持Map的FormHttpMessageConverter即可,内部可调用FormHttpMessageConverter的方法简化操作。

*支持文件上传:只需要把要上传的文件封装成一个Resource(该Resource一定要实现filename接口,这个是把请求参数解析成文件的标识),使用默认的ResourceHttpMessageConverter处理即可。

*支持处理MultipartFile参数:编写一个支持MultipartFile的MultipartFileHttpMessageConverter即可,内部可调用ResourceHttpMessageConverter实现,同时注意需要将其添加至FormHttpMessageConverter的Parts中,并重写FormHttpMessageConverter的getFilename方法支持从MultipartFile中获取filename

*所有的HttpMessageConverter直接以@Bean的方式生成即可,spring会自动识别添加

完美支持表单和文件上传:

方案一:

使用附件中的MapFormHttpMessageConverter.java和MultipartFileHttpMessageConverter.java

在Spring中进行如下配置即可

@Bean

public MapFormHttpMessageConverter mapFormHttpMessageConverter(MultipartFileHttpMessageConverter multipartFileHttpMessageConverter) {

MapFormHttpMessageConverter mapFormHttpMessageConverter = new MapFormHttpMessageConverter();

mapFormHttpMessageConverter.addPartConverter(multipartFileHttpMessageConverter);

return mapFormHttpMessageConverter;

}

@Bean

public MultipartFileHttpMessageConverter multipartFileHttpMessageConverter() {

return new MultipartFileHttpMessageConverter();

}

方案二:

使用FeignSpringFormEncoder.java

在Spring中配置如下:

@Bean

public Encoder feignEncoder(ObjectFactory messageConverters) {

return new FeignSpringFormEncoder(messageConverters);

}

推荐使用方案一

方案二为参考https://github.com/pcan/feign-client-test而来,未测


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java中BIO、NIO、AIO的理解
下一篇:云平台管理平台接口(云平台管理服务器)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~