Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I generate a Spring Feign client with Multipart parameters?

I am getting the error: "Method has too many Body parameters" when trying to generate a Spring Feign client

@RequestMapping(value="/media", method=RequestMethod.POST)
String uploadMedia(@RequestHeader("Authentication") String token,
    @RequestPart("media") MultipartFile audio, 
    @RequestPart("a-json-object") SomeClass someClazz,
    @RequestPart("another-json-object") AnotherClass anotherClazz);

I found the following solution, which works when using regular Feign annotations, but not with Spring MVC annotations:

'Too many body parameters' Exception on Feign Client

like image 380
cerebrotecnologico Avatar asked May 09 '16 16:05

cerebrotecnologico


People also ask

Which is better feign client or RestTemplate?

One of the advantages of using Feign over RestTemplate is that, we do not need to write any implementation to call the other services. So there is no need to write any unit test as there is no code to test in the first place.

How do you connect two Microservices using Feign client?

Let's implement the Feign in our project and invoke other microservices using Feign. Step 1: Select currency-conversion-service project. Step 2: Open the pom. xml and add the Feign dependency.

Is feign client synchronous or asynchronous?

Synchronous and Asynchronous API callsThe API that we defined via Feign is synchronous — meaning that it makes blocking calls to the server. Feign allows us to easily define async APIs as well — utilizing CompletableFutures under the hood.


1 Answers

It should be possible now. Add the following dependencies:

<dependencies>
...
<dependency>
    <groupId>io.github.openfeign.form</groupId>
    <artifactId>feign-form</artifactId>
    <version>2.2.0</version>
</dependency>
<dependency>
    <groupId>io.github.openfeign.form</groupId>
    <artifactId>feign-form-spring</artifactId>
    <version>2.2.0</version>
</dependency>
...

and use this client configuration:

@FeignClient(name = "file-upload-service", configuration = FileUploadServiceClient.MultipartSupportConfig.class)
public interface FileUploadServiceClient extends IFileUploadServiceClient {

    @Configuration
    public class MultipartSupportConfig {

        @Bean
        @Primary
        @Scope("prototype")
        public Encoder feignFormEncoder() {
            return new SpringFormEncoder();
        }
    }
}

Example was taken from: feign-form docs

like image 87
D-rk Avatar answered Oct 21 '22 09:10

D-rk