Tag Archives: Problems solved

SpringCloud Use openFeign Multipartfile to Upload Files Error: Current request is not a multipart request

Error reporting information

feign MultipartException: Current request is not a multipart request

 

On the premise of introducing and configuring openfeign

1. Create FeignMultipartSupportConfig.class configuration file

Just copy and paste it directly

import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;

@Configuration
public class FeignMultipartSupportConfig {

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

    @Bean
    public feign.Logger.Level multipartLoggerLevel() {
        return feign.Logger.Level.FULL;
    }

}

2. Modify postmapping

    1. 1. MultipartFile type parameters use @RequestPart annotation, string parameters use annotation @RequestParam
    1. 2. Add consumes = MediaType.MULTIPART_FORM_DATA_VALUE
@FeignClient(value = "mk-other", fallback = MkOtherFeignFallBack.class)
public interface MkOtherFeign {
    @PostMapping(value = "/file/pic/upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseResult picUpload(@RequestPart("file") MultipartFile file);
}

Reference link