随笔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 <FileUploadResponse> 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 <FileUploadResponse> 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

        <dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form-spring</artifactId>
            <version>3.3.0</version>
        </dependency>
        
        <dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form</artifactId>
            <version>3.3.0</version>
        </dependency>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容