基于feign传参MultipartFile问题解决

 更新时间:2022年03月04日 11:28:47   作者:qq_42151769  
这篇文章主要介绍了基于feign传参MultipartFile问题解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

feign传参MultipartFile问题

首先,feign服务之间的调用,传参默认的格式为:ContentType=application/x-www-form-urlencoded

以表单的形式传参的,而文件流的传参,需要form-data的ContentType,否则会报错的

首先引入依赖

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

注意spring boot版本是2.x以上的,上面两个依赖的版本不低于3.5.0,否则还是无效的

新建feign的配置

package com.wm.blog_config.config; 
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; 
 
/**
 * @author :半卷流年
 * @description : 解决feign传递流数据的异常
 * @createTime :2020/6/14
 */
@Configuration
public class FeignSupportConfig { 
    @Autowired
    private ObjectFactory<HttpMessageConverters> messageConverters;
 
    @Bean
    public Encoder feignFormEncoder() {
        return new SpringFormEncoder(new SpringEncoder(messageConverters));
    } 
}

在feign接口中配置

package com.wm.blog_admin.feign; 
import com.wm.blog_admin.feign.factory.PictureClientFallbackFactory;
import com.wm.blog_common.constatnt.CommonConstant;
import com.wm.blog_common.domain.TFileDO;
import com.wm.blog_common.entity.TFile;
import com.wm.blog_common.req.TFileQuery;
import com.wm.blog_common.result.Result;
import com.wm.blog_config.config.CustomFeignConfig;
import com.wm.blog_config.config.FeignSupportConfig;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; 
import java.util.List;
 
/***
 * @ClassName: PictureFeignClient
 * @Description: picture feign调用  todo feign使用get有坑啊,是否考虑使用HttpClient替换feign的HttpURLConnection,采用apache的HttpClient
 * @Author: wm_yu
 * @Create_time: 16:39 2020-3-26
 */
@FeignClient(value = CommonConstant.PICTURE_MODULE_NAME, configuration = {CustomFeignConfig.class, FeignSupportConfig.class}, fallbackFactory = PictureClientFallbackFactory.class)
public interface PictureFeignClient {
 
    /**
     * id查询图片信息
     * @param id
     * @return
     */
    @GetMapping("/web/picture/{id}")
    Result<TFileDO> get(@PathVariable("id") Long id); 
 
    /**
     * id批量查询图片信息
     * @param idList
     * @return
     */
    @PostMapping("/web/picture/getByIdList")
    Result<List<TFile>> getByIdList(@RequestBody List<Long> idList); 
 
    /**
     * 文件上传
     * @param file
     * @return
     */
    @PostMapping(value = "/web/picture/uploadFile",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
     Result<String> uploadFile(@RequestPart("file") MultipartFile file);
 
}

注意加上这个,表示传参格式:

就可以传参了的

Feign传输 MultipartFile的一些问题

File转MultipartFile

pom.xml

<!-- https://mvnrepository.com/artifact/org.springframework/spring-mock -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-mock</artifactId>
<version>2.0.8</version>
</dependency>
public static MultipartFile getMultipartFile(String fileName, File file) throws IOException {
    return new MockMultipartFile(fileName, file.getName(), ContentType.APPLICATION_OCTET_STREAM.toString(), new FileInputStream(file));
}

报错 Current request is not a multipart request、Content type ‘’ not supported

@PostMapping设置 consumes = MediaType.MULTIPART_FORM_DATA_VALUE

使用@RequestPart(),不能使用@RequestParam()

@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
ResultBody upload(@RequestPart(value = "file") MultipartFile file);

报错 Required request part ‘file’ is not present

configuration

@Configuration
public class UploadFeignConfig {
    @Bean
    public Encoder multipartFormEncoder() {
        return new SpringFormEncoder(new SpringEncoder(new ObjectFactory<HttpMessageConverters>() {
            @Override
            public HttpMessageConverters getObject() throws BeansException {
                return new HttpMessageConverters(new RestTemplate().getMessageConverters());
            }
        }));
    }
}

FeignClient

@FeignClient(value = FileConstants.FILE_SERVER, configuration= UploadFeignConfig.class)
public interface FileServiceClient extends IFileServiceClient {
    @Override
    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    ResultBody upload(@RequestPart(value = "file") MultipartFile file);
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Java中Map集合的常用方法(非常详细!)

    Java中Map集合的常用方法(非常详细!)

    Java中的Map是一种键值对存储的数据结构,它提供了快速查找和访问数据的能力,下面这篇文章主要给大家介绍了关于Java中Map集合的常用方法,需要的朋友可以参考下
    2024-01-01
  • 关于SpringMVC在Controller层方法的参数解析详解

    关于SpringMVC在Controller层方法的参数解析详解

    在SpringMVC中,控制器Controller负责处理由DispatcherServlet分发的请求,下面这篇文章主要给大家介绍了关于SpringMVC在Controller层方法的参数解析的相关资料,需要的朋友可以参考下
    2021-12-12
  • java中String StringBuffer和StringBuilder的区别详解

    java中String StringBuffer和StringBuilder的区别详解

    大家好,本篇文章主要讲的是java中String StringBuffer和StringBuilder的区别详解,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下
    2022-01-01
  • Java异常的处理机制

    Java异常的处理机制

    这篇文章主要介绍了Java异常的处理机制,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-09-09
  • Java中int和Integer的区别

    Java中int和Integer的区别

    这篇文章主要介绍的是 Java中int和Integer的区别,Java 是一种强数据类型的语言,因此所有的属性必须有一个数据类型,下面文章基于Java详细int和Integer有何区别,需要的朋友可以参考一下
    2021-11-11
  • Spring Boot文件上传原理与实现详解

    Spring Boot文件上传原理与实现详解

    这篇文章主要介绍了Spring Boot 文件上传原理与实现详解,前端文件上传是面向多用户的,多用户之间可能存在上传同一个名称、类型的文件;为了避免文件冲突导致的覆盖问题这些应该在后台进行解决,需要的朋友可以参考下
    2024-01-01
  • Hibernate基于ThreadLocal管理Session过程解析

    Hibernate基于ThreadLocal管理Session过程解析

    这篇文章主要介绍了Hibernate基于ThreadLocal管理Session过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-10-10
  • springboot注入servlet的方法

    springboot注入servlet的方法

    本篇文章主要介绍了springboot注入servlet的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-05-05
  • springboot项目docker分层构建的配置方式

    springboot项目docker分层构建的配置方式

    在使用dockerfile构建springboot项目时,速度较慢,用时比较长,为了加快构建docker镜像的速度,采用分层构建的方式,这篇文章主要介绍了springboot项目docker分层构建,需要的朋友可以参考下
    2024-03-03
  • java获取本地文件的多种方式实现与比较

    java获取本地文件的多种方式实现与比较

    这篇文章主要为大家详细介绍了java获取本地文件的多种方式实现与结果比较,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-11-11

最新评论