Feign远程调用Multipartfile参数
今天在写业务代码的时候遇到的问题, 前端请求A服务,能正确把参数给到A服务<参数里面包括文件类型的 Multipartfile>,但是当A服务调用B服务时把文件参数丢过去,那边接收不到,并且会报Multipartfile 参数错误。
在网上找到了答案,特此记录一下:
解决方案
必须的
在你的fegin 服务接口暴露的地方新建一个配置类。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
package com.gobon.gbjk.file.api.config; import feign.form.spring.SpringFormEncoder; import org.springframework.beans.factory.ObjectFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.http.HttpMessageConverters; import org.springframework.cloud.openfeign.support.SpringEncoder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * <p> * feign配置类 * </p> * * @projectName: gbjk-user * @package: com.gobon.gbjk.file.api.config * @className: FeignSupportConfig * @author: wen jun tang * @date: 2021年12月21日 12:25 * @version: 1.0 */ @Configuration public class FeignSupportConfig { @Autowired private ObjectFactory<HttpMessageConverters> messageConverters; @Bean public SpringFormEncoder feignFormEncoder() { return new SpringFormEncoder( new SpringEncoder(messageConverters)); } } |
然后你的接口 文件参数用@RequestPart修饰:
1
2
|
@PostMapping (value = "/file/upload" , consumes = MediaType.MULTIPART_FORM_DATA_VALUE) AjaxResult upload( @RequestPart ( "file" ) MultipartFile file, @RequestParam ( "bucketName" ) String bucketName); |
再次调用, 完美解决!
Feign远程调用传参问题
如果是简单参数,用@RequestParam或者@PathVariable注解
如果是对象参数,用@RequestBody注解
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/Ambitiouss/article/details/122069471