3

I'm having a problem uploading an image using feign. I have multiple services using spring cloud. Version of my dependencies below

spring boot - 1.4.3.RELEASE
spring-cloud-starter-feign - 1.1.3.RELEASE
io.github.openfeign.form - 2.2.1
io.github.openfeign.form - 2.2.1

In my form I have fields with a Multipartfile ex below

public class MyFrom {
    private String field1;
    private String field2;
    private MultipartFile image;

    //getters and setters
}

And passing it in my feign client

@RequestMapping(value = { "/api/some-task},
        method = RequestMethod.POST,
        consumes = {"multipart/form-data"})
ResponseEntity<MyForm> addPromoTask(@RequestBody MyForm request);

I already added a SpringFormEncoder in my code but I've check the encoder's code but it doesn't seem to support when Multipartfile is included in the RequestBody.

@FeignClient(value = "some-feign",
    fallback = SomeTaskClient.SomeTaskClienttFallback.class,
    configuration = SomeTaskClient.CoreFeignConfiguration.class)
public interface SomeTaskClient extends SomeTaskApi {

    @Configuration
    class CoreFeignConfiguration {

        @Bean
        @Primary
        @Scope(SCOPE_PROTOTYPE)
        Encoder feignFormEncoder() {
            return new SpringFormEncoder();
        }
    }
}

I have seen that you can pass multiple @RequestPart in the link below but I can't seem to make it work. I get an error where it says I'm passing multiple body parameters.

https://github.com/bilak/spring-multipart-feign-poc/blob/master/src/main/java/com/github/bilak/poc/ContentClient.java

rhandom
  • 1,198
  • 3
  • 10
  • 15

2 Answers2

4

1.You need to upgrade the dependent version of the feign form in the pom.xml file

<dependency>
   <groupId>io.github.openfeign.form</groupId>
   <artifactId>feign-form</artifactId>
   <version>3.0.0</version>
</dependency>
<dependency>
   <groupId>io.github.openfeign.form</groupId>
   <artifactId>feign-form-spring</artifactId>
   <version>3.0.0</version>
</dependency>
  1. Modify the configuration client as follows

@FeignClient(name = "service1", configuration = {MultipartSupportConfig.class})
public interface FileUploadServiceClient {
    @RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = MULTIPART_FORM_DATA_VALUE)
    public @ResponseBody String handleFileUpload(
                            @RequestPart(value = "file", required = true) MultipartFile file,
                            @RequestParam(value = "name") String name) throws IOException;

    @Configuration
    public class MultipartSupportConfig {

        @Autowired
        private ObjectFactory<HttpMessageConverters> messageConverters;

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

Issues reference https://github.com/OpenFeign/feign-form/issues/19

GuoGuang
  • 41
  • 6
  • Hello. thanks for this solution. I had one question. Why is the feignEncoder bean scoped prototye ? Could this work as a singleton? Thanks – user7510999 Nov 03 '22 at 11:47
3

Maybe you should use 'consumes' in the mapping anotation, this worked for me for spring boot 2 and spring-cloud-starter-openfeign:

   @PostMapping(value="/upload", consumes = "multipart/form-data" )
   QtiPackageBasicInfo upload(@RequestPart("package") MultipartFile package);
MBozic
  • 1,132
  • 1
  • 10
  • 22