随笔2018-09-22 SpringCloud Feign下如何传输文件

服务提供者:upload-server

context-path: /uploadService
public class UploadController {
   //other methods...

    @PostMapping(value = "/file/upload",

        consumes = {MediaType.MULTIPART_FORM_DATA_VALUE}, 

        produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})

    public ResponseEntity  uploadFile (

       @RequestParam("file") MultipartFilefile

   );
}

服务消费者:upload-client

@FeignClient(name = "upload-server", path="/uploadService", configuration=MultipartFileSupportConfig.class)
public class UploadClient {
     // other methods

    @PostMapping(value = "/file/upload",

        consumes = {MediaType.MULTIPART_FORM_DATA_VALUE}, 

        produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})

    public ResponseEntity  uploadFile(

        @RequestPart("file") MultipartFilefile

    );

}

MultipartFileSupportConfig.class

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;

import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;

@Configuration
public class MultipartFileSupportConfig {
        
    @Bean
    @Primary
    @Scope("prototype")
    public Encoder multipartFormEncoder() {     
        return new SpringFormEncoder();
    }
 
    @Bean
    public feign.Logger.Level multipartLoggerLevel() {
        return feign.Logger.Level.FULL;
    }
    
    
}

记得引入Dependency

        
            io.github.openfeign.form
            feign-form-spring
            3.3.0
        
        
        
            io.github.openfeign.form
            feign-form
            3.3.0
        

你可能感兴趣的:(随笔2018-09-22 SpringCloud Feign下如何传输文件)