使用feign传递参数类型为MultipartFile的问题

目录
  • feign传递参数类型为MultipartFile
    • 引入maven依赖
    • 加入配置类
    • 在feign客户端进行配置
  • feign传参MultipartFile问题解决
    • 首先引入依赖
    • 新建feign的配置
    • 在feign接口中配置

feign传递参数类型为MultipartFile

feign默认是不支持多媒体文件类型的文件传输的,但是可以通过引入第三方jar包解决这个问题,步骤可以分为三步。

引入maven依赖

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

加入配置类

@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;
    }
}

在feign客户端进行配置

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;
import config.FeignMultipartSupportConfig;
import feign.Response;
@FeignClient(value = "", fallback = FileServiceFallback.class,configuration=FeignMultipartSupportConfig.class)
public interface IFileService {
    //上传文件
    @RequestMapping(value = "/rmi/fileService/mediaImgUpload",  produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String mediaImgUpload(@RequestPart MultipartFile file);
    //下载文件
    @RequestMapping(value = "/rmi/fileService/mediaDownload",method = RequestMethod.GET,consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Response mediaDownload(@RequestParam(required = true) String mediaId);

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);
}

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

就可以传参了的

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

(0)

相关推荐

  • Feign之Multipartfile文件传输填坑

    Multipartfile文件传输 1. 添加依赖 <dependency> <groupId>io.github.openfeign.form</groupId> <artifactId>feign-form</artifactId> <version>3.3.0</version> </dependency> <dependency> <groupId>io.github.openf

  • feign实现传递参数的三种方式小结

    需要注意的一点是,feign好像是无法传递list集合类型的,但是你可以通过传递对象类型,然后在接收方再次将对象装在集合中达到集合传递的效果 传递方式一:传递的都是基本数据类型 restful风格参数,用@PathVariable写着走就行了 传递方式二:传递数组类型的参数 不使用restful风格,直接用@RequestParam声明参数之间的对应关系. 传递方式三:传递带有对象的参数 1.使用restful风格的参数要用@Pathvarible声明参数对应关系,@Pathvariable用于

  • SpringCloud Feign参数问题及解决方法

    这篇文章主要介绍了SpringCloud Feign参数问题及解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 今天遇到使用Feign调用微服务,传递参数时遇到几个问题 1.无参数 以GET方式请求 服务提供者 @RequestMapping("/hello") public String Hello(){ return "hello,provider"; } 服务消费者 @GetMapping("

  • SpringCloud Feign多参数传递及需要注意的问题

    目录 Feign多参数传递及注意的问题 在服务提供者cloud-shop-userservice中新增几个方法 修改feign的UserService,新增对应的方法 在feign的controller中调用方法 重启修改过的服务,查看服务注册是否正常 使用工具调用这几个方法进行测试 Feign如何接收多个参数 1.API 2.Feign 3.controller Feign多参数传递及注意的问题 这边沿用前面的Eureka,Feign,Service 在服务提供者cloud-shop-user

  • 使用feign传递参数类型为MultipartFile的问题

    目录 feign传递参数类型为MultipartFile 引入maven依赖 加入配置类 在feign客户端进行配置 feign传参MultipartFile问题解决 首先引入依赖 新建feign的配置 在feign接口中配置 feign传递参数类型为MultipartFile feign默认是不支持多媒体文件类型的文件传输的,但是可以通过引入第三方jar包解决这个问题,步骤可以分为三步. 引入maven依赖         <dependency>             <group

  • 详解Mybatis 传递参数类型为List的取值问题

    问题描述: 参数传递为List时: 当传递一个 List 实例或者数组作为参数对象传给 Mybatis.此时,Mybatis 会自动将它包装在一个 Map 中,用名称在作为键.List 实例将会以"list" 作为键,而数组实例将会以"array"作为键.所以,当我们传递的是一个List集合时,mybatis会自动把我们的list集合包装成以list为Key值的map. DAO 层: List<User> selectUserByIDs( List ID

  • 微服务如何通过feign.RequestInterceptor传递参数

    目录 微服务通过feign.RequestInterceptor传递参数 创建自定义请求拦截器 具体获取逻辑如下 feign的拦截器RequestInterceptor 首先创建自定义的RequestInterceptor 然后定义一个feign的接口类 再创建一个controller 运行程序测试一下接口 查看程序打印 微服务通过feign.RequestInterceptor传递参数 Feign 支持请求拦截器,在发送请求前,可以对发送的模板进行操作,例如设置请求头等属性,自定请求拦截器需要

  • OpenFeign在传递参数为对象类型是为空的问题

    目录 OpenFeign传递参数为对象类型是为空 解决的方法很简单 使用OpenFeign服务调用传参为null 正确为 OpenFeign传递参数为对象类型是为空 今天在使用OpenFeign时发现当传递参数为对象类型时,接受到的参数为null. 解决的方法很简单 在参数前面加上@RequestBody即可 @PostMapping("/account/update")     public CommonResult update(@RequestBody TOrder order)

  • SpringCloud通过Feign传递List类型参数方式

    目录 通过Feign传递List类型参数 1.单个List实体传递 2.基本类型传递 3.实体类型传递 Feign在参数为List时的坑 错误写法 正确写法 通过Feign传递List类型参数 首先明确一点,SpringCloud通过Fegin如果是多个参数,其中一个参数是List,那么是传不过去的,单个List是可以的. 1.单个List实体传递 @RequestMapping("/secret/batchInsert") public int batchInsert(@Reques

  • Javascript基于AJAX回调函数传递参数实例分析

    本文实例讲述了Javascript基于AJAX回调函数传递参数的方法.分享给大家供大家参考,具体如下: 前面介绍了<javascript实现html页面之间参数传递的四种方法>,这里针对ajax参数传递做一分析. 在Javascript 中,特别是在AJAX中,回调函数常常是一个函数名,没有地方放入参数,如下面的AJAX代码,在成功后将调用回调函数callback,但callback是有参数的,如何把参数传进来呢? var callback = function(p1){ //do somet

  • Spring Mvc中传递参数方法之url/requestMapping详解

    前言 相信大家在使用spring的项目中,前台传递参数到后台是经常遇到的事, 我们必须熟练掌握一些常用的参数传递方式和注解的使用,本文将给大家介绍关于Spring Mvc中传递参数方法之url/requestMapping的相关内容,分享出来供大家参考学习,话不多说,直接上正文. 方法如下 1. @requestMapping: 类级别和方法级别的注解, 指明前后台解析的路径. 有value属性(一个参数时默认)指定url路径解析,method属性指定提交方式(默认为get提交) @Reques

  • Python函数参数类型*、**的区别

    刚开始学习python,python相对于java确实要简洁易用得多.内存回收类似hotspot的可达性分析, 不可变对象也如同java得Integer类型,with函数类似新版本C++的特性,总体来说理解起来比较轻松.只是函数部分参数的"*"与"**",闭包等问题,着实令人迷糊了一把,弄清概念后写下此文记录下来,也希望本文能够帮助其他初学者. 所以本文是一篇学习笔记,着重于使用的细节和理解上,首先分别介绍了函数各种参数类型在调用和声明时的区别,及其在混用时需要注意

  • c#线程间传递参数详解

    线程操作主要用到Thread类,他是定义在System.Threading.dll下.使用时需要添加这一个引用.该类提供给我们四个重载的构造函数(以下引自msdn). Thread (ParameterizedThreadStart)  初始化 Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托.  Thread (ThreadStart)  初始化 Thread 类的新实例. Thread (ParameterizedThreadStart, Int32)  初始化 Threa

随机推荐