zoukankan      html  css  js  c++  java
  • SpringCloud使用Feign Form实现微服务之间的文件上传

    1.pom依赖

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

    2.MultipartSupportConfig配置类

    @Configuration
    public class MultipartSupportConfig {
        @Autowired
        private ObjectFactory<HttpMessageConverters> messageConverters;
    
        @Bean
        public Encoder feignFormEncoder() {
            return new SpringFormEncoder(new SpringEncoder(messageConverters));
        }
    }

    3.后端接口改造

    关键字consumes = MULTIPART_FORM_DATA_VALUE

    /**
    * 文件上传接口
    * @param file 上传文件
    * @date 2019-11-19 11:56:40
    **/
    @PostMapping(value = "uploadExamTarget", consumes = MULTIPART_FORM_DATA_VALUE)
    public JsonResult uploadExamTarget(@RequestBody MultipartFile file) {
    return new JsonResult(coreClient.uploadExamTarget(file));
    }

    4.FeignClient接口改造

    引入配置类MultipartSupportConfig,和接口注解consumes = MULTIPART_FORM_DATA_VALUE

    @FeignClient(value = "core-server", configuration = MultipartSupportConfig.class)
    public interface CoreClient {  
      /**
       * 调用core-server服务的文件上传接口
       * @param file 上传文件
      * @date 2019-11-19 11:56:40
      **/
      @RequestMapping(value = "/excel/uploadExamTarget", consumes = MULTIPART_FORM_DATA_VALUE) 
      String uploadExamTarget(@RequestBody MultipartFile file);
    }
  • 相关阅读:
    【禅道】禅道安装步骤
    软件测试学习路线
    【mysql】mysql数据库安装
    【用例】测试用例阶段总结
    【坑】自动化测试之Excel表格
    开始.....
    网络攻防
    PAT/查找元素习题集
    PAT/简单模拟习题集(二)
    PAT/简单模拟习题集(一)
  • 原文地址:https://www.cnblogs.com/shiblog/p/11888398.html
Copyright © 2011-2022 走看看