OpenFeign实现微服务间的文件上传

介绍

解决feign实现微服务间的文件上传出现错误the request was rejected because no multipart boundary was found

引入包

    <!-- Feign进行跨服务传递文件依赖 -->
    <dependency>
        <groupId>io.github.openfeign.form</groupId>
        <artifactId>feign-form</artifactId>
        <version>3.8.0</version>
    </dependency>
    <dependency>
        <groupId>io.github.openfeign.form</groupId>
        <artifactId>feign-form-spring</artifactId>
        <version>3.8.0</version>
    </dependency>

这是Feign拓展包,版本选择:

Requirements The feign-form extension depend on OpenFeign and its
concrete versions:

all feign-form releases before 3.5.0 works with OpenFeign 9.*
versions; starting from feign-form’s version 3.5.0, the module works
with OpenFeign 10.1.0 versions and greater. IMPORTANT: there is no
backward compatibility and no any gurantee that the feign-form’s
versions after 3.5.0 work with OpenFeign before 10.*. OpenFeign was
refactored in 10th release, so the best approach - use the freshest
OpenFeign and feign-form versions.

Notes:

spring-cloud-openfeign uses OpenFeign 9.* till v2.0.3.RELEASE and uses
10.* after. Anyway, the dependency already has suitable feign-form version, see dependency pom, so you don’t need to specify it
separately;

spring-cloud-starter-feign is a deprecated dependency and it always
uses the OpenFeign’s 9.* versions.
需要特别注意 feign-form 和 OpenFeign 版本之间的关系,spring boot 2.0 至少是3.5.0。

服务接口端

@ApiOperation(value = "附件上传")
@PostMapping("/upload/v1")
public AttachmentOutDTO upload(@RequestParam(value = "file") MultipartFile file,
                               @RequestParam(value = "module", required = false) String module) {
     							//附件上传业务
}

Feign调用端

@Component
@FeignClient(value = "service-file", path = "/api/attachment", fallback = AttachmentClientHystrix.class, configuration = MultipartSupportConfig.class)
public interface AttachmentClient {
    /**
     * 附件上传
     *
     * @param file
     * @param module
     * @return
     */
    @PostMapping(value = "/upload/v1", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    AttachmentOutDTO upload(@RequestPart(value = "file") MultipartFile file,
                            @RequestParam(value = "module", required = false) String module);
}

配置端

@Configuration
public class MultipartSupportConfig {

    @Autowired
    private ObjectFactory<HttpMessageConverters> messageConverters;

    @Bean
    public Encoder feignFormEncoder(){
        return new SpringFormEncoder(new SpringEncoder(messageConverters));
    }

}

特别注意:

  1. consumes = MediaType.MULTIPART_FORM_DATA_VALUE必须有

  2. 文件部分注解为@RequestPart,要不然出现错误:the request was rejected because no
    multipart boundary was found

  3. MultipartSupportConfig 不可以少。

参考:https://blog.csdn.net/y526089989/article/details/97762675

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr-Wanter

感谢大佬

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值