feign传参MultipartFile问题如何解决
本篇内容介绍了“feign传参MultipartFile问题如何解决”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
feign传参MultipartFile问题
首先,feign服务之间的调用,传参默认的格式为:ContentType=application/x-www-form-urlencoded
以表单的形式传参的,而文件流的传参,需要form-data的ContentType,否则会报错的
首先引入依赖
<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>
注意spring boot版本是2.x以上的,上面两个依赖的版本不低于3.5.0,否则还是无效的
新建feign的配置
package com.wm.blog_config.config; import feign.codec.Encoder;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; @Configurationpublic class FeignSupportConfig { @Autowired private ObjectFactory<HttpMessageConverters> messageConverters; @Bean public Encoder feignFormEncoder() { return new SpringFormEncoder(new SpringEncoder(messageConverters)); } }
在feign接口中配置
package com.wm.blog_admin.feign; import com.wm.blog_admin.feign.factory.PictureClientFallbackFactory;import com.wm.blog_common.constatnt.CommonConstant;import com.wm.blog_common.domain.TFileDO;import com.wm.blog_common.entity.TFile;import com.wm.blog_common.req.TFileQuery;import com.wm.blog_common.result.Result;import com.wm.blog_config.config.CustomFeignConfig;import com.wm.blog_config.config.FeignSupportConfig;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.http.MediaType;import org.springframework.web.bind.annotation.*;import org.springframework.web.multipart.MultipartFile; import java.util.List; @FeignClient(value = CommonConstant.PICTURE_MODULE_NAME, configuration = {CustomFeignConfig.class, FeignSupportConfig.class}, fallbackFactory = PictureClientFallbackFactory.class)public interface PictureFeignClient { @GetMapping("/web/picture/{id}") Result<TFileDO> get(@PathVariable("id") Long id); @PostMapping("/web/picture/getByIdList") Result<List<TFile>> getByIdList(@RequestBody List<Long> idList); @PostMapping(value = "/web/picture/uploadFile",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},consumes = MediaType.MULTIPART_FORM_DATA_VALUE) Result<String> uploadFile(@RequestPart("file") MultipartFile file); }
注意加上这个,表示传参格式:
就可以传参了的
Feign传输 MultipartFile的一些问题
File转MultipartFile
pom.xml
<!-- https://mvnrepository.com/artifact/org.springframework/spring-mock --><dependency><groupId>org.springframework</groupId><artifactId>spring-mock</artifactId><version>2.0.8</version></dependency>
public static MultipartFile getMultipartFile(String fileName, File file) throws IOException { return new MockMultipartFile(fileName, file.getName(), ContentType.APPLICATION_OCTET_STREAM.toString(), new FileInputStream(file));}
报错 Current request is not a multipart request、Content type ‘’ not supported
@PostMapping设置 consumes = MediaType.MULTIPART_FORM_DATA_VALUE
使用@RequestPart(),不能使用@RequestParam()
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)ResultBody upload(@RequestPart(value = "file") MultipartFile file);
报错 Required request part ‘file’ is not present
configuration
@Configurationpublic class UploadFeignConfig { @Bean public Encoder multipartFormEncoder() { return new SpringFormEncoder(new SpringEncoder(new ObjectFactory<HttpMessageConverters>() { @Override public HttpMessageConverters getObject() throws BeansException { return new HttpMessageConverters(new RestTemplate().getMessageConverters()); } })); }}
FeignClient
@FeignClient(value = FileConstants.FILE_SERVER, configuration= UploadFeignConfig.class)public interface FileServiceClient extends IFileServiceClient { @Override @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) ResultBody upload(@RequestPart(value = "file") MultipartFile file);}
“feign传参MultipartFile问题如何解决”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注编程网网站,小编将为大家输出更多高质量的实用文章!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341