Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of feign parameter transfer MultipartFile

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to solve the MultipartFile problem of feign parameter transfer". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

MultipartFile problem of feign parameter transfer

First, calls between feign services. The default format for passing parameters is: ContentType=application/x-www-form-urlencoded.

Parameters are passed in the form of a form, while the parameters of the file stream need the ContentType of form-data, otherwise an error will be reported.

First introduce dependency io.github.openfeign.form feign-form 3.8.0 io.github.openfeign.form feign-form-spring 3.8.0

Note that the spring boot version is above 2.x, and the above two dependent versions are not less than 3.5.0, otherwise they are still invalid.

Create a new configuration 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 for feign / * * @ author: half Volume year * @ description: solve the exception of feign transferring stream data * @ createTime: 2020-6-14 * / @ Configurationpublic class FeignSupportConfig {@ Autowired private ObjectFactory messageConverters; @ Bean public Encoder feignFormEncoder () {return new SpringFormEncoder (new SpringEncoder (messageConverters));} configure package com.wm.blog_admin.feign; import com.wm.blog_admin.feign.factory.PictureClientFallbackFactory in the feign interface 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 / * @ ClassName: PictureFeignClient * @ Description: when picture feign calls todo feign, there is a hole in using get. Do you consider using HttpClient instead of HttpURLConnection in feign? Use apache's HttpClient * @ Author: wm_yu * @ Create_time: 16:39 2020-3-26 * / @ FeignClient (value = CommonConstant.PICTURE_MODULE_NAME, configuration = {CustomFeignConfig.class, FeignSupportConfig.class}, fallbackFactory = PictureClientFallbackFactory.class) public interface PictureFeignClient {/ * id query picture information * @ param id * @ return * / @ GetMapping ("/ web/picture/ {id}") Result get (@ PathVariable ("id") Long id) / * id batch query image information * @ param idList * @ return * / @ PostMapping ("/ web/picture/getByIdList") Result getByIdList (@ RequestBody List idList) / * * File upload * @ param file * @ return * / @ PostMapping (value = "/ web/picture/uploadFile", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) Result uploadFile (@ RequestPart ("file") MultipartFile file);}

Note that this indicates the format of passing parameters:

You can pass the parameters.

Some problems of Feign Transmission MultipartFile File to MultipartFile

Pom.xml

Org.springframeworkspring-mock2.0.8public static MultipartFile getMultipartFile (String fileName, File file) throws IOException {return new MockMultipartFile (fileName, file.getName (), ContentType.APPLICATION_OCTET_STREAM.toString (), new FileInputStream (file));}

Error Current request is not a multipart request, Content type''not supported

@ PostMapping sets consumes = MediaType.MULTIPART_FORM_DATA_VALUE

Use @ RequestPart (), not @ RequestParam ()

@ PostMapping (value = "/ upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) ResultBody upload (@ RequestPart (value = "file") MultipartFile file)

Error Required request part 'file' is not present

Configuration

@ Configurationpublic class UploadFeignConfig {@ Bean public Encoder multipartFormEncoder () {return new SpringFormEncoder (new SpringEncoder (new ObjectFactory () {@ 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);} "how to solve the MultipartFile problem of feign passing parameters" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report