Get the App
SLTechnology News&Howtos  ›  Development  › 

How to handle the Multipartfile parameter of Feign remote call

Shulou Source: shulou.com Published: 2022-06-01 08:20:36 10月26日 Update

Today, I would like to share with you the relevant knowledge points about how to deal with Feign remote call Multipartfile parameters. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.

Feign remote call Multipartfile parameter

The problem encountered when writing business code today is that the front end requests A service to give the parameters to A service correctly, but when A service invokes B service, it throws the file parameters, which can not be received there, and will report an error in Multipartfile parameters.

The answer has been found on the Internet, and here is a record:

Solution

It has to be.

Create a new configuration class where your fegin service interface is exposed.

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;/** *

* feign configuration class *

* * @ projectName: gbjk-user * @ package: com.gobon.gbjk.file.api.config * @ className: FeignSupportConfig * @ author: wen jun tang * @ date: 12:25, December 21, 2021 * @ version: 1.0 * / @ Configurationpublic class FeignSupportConfig {@ Autowired private ObjectFactory messageConverters; @ Bean public SpringFormEncoder feignFormEncoder () {return new SpringFormEncoder (new SpringEncoder (messageConverters));}}

Then modify your interface file parameters with @ RequestPart:

@ PostMapping (value = "/ file/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) AjaxResult upload (@ RequestPart ("file") MultipartFile file, @ RequestParam ("bucketName") String bucketName)

Call again, perfect solution!

The problem of passing parameters in Feign remote call

If it is a simple parameter, annotate with @ RequestParam or @ PathVariable

If it is an object parameter, annotate with @ RequestBody

These are all the contents of the article "how to handle the Multipartfile parameters of Feign remote calls". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Tags: Parameters services knowledge articles content interfaces files comments questions configurations different big business code again front end place most objects that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Docker macOS Apple Shulou Tech Info