上传文件调用外部服务报错: not a type supported by this encoder

查看SpringFormEncoder类的源码:

 public class SpringFormEncoder extends FormEncoder
{ public SpringFormEncoder()
{
this(((Encoder) (new feign.codec.Encoder.Default())));
} public SpringFormEncoder(Encoder delegate)
{
super(delegate);//调用父类的构造方法
MultipartFormContentProcessor processor = (MultipartFormContentProcessor)getContentProcessor(ContentType.MULTIPART);
processor.addWriter(new SpringSingleMultipartFileWriter());
processor.addWriter(new SpringManyMultipartFilesWriter());
} public void encode(Object object, Type bodyType, RequestTemplate template)
throws EncodeException
{
if(!bodyType.equals(org/springframework/web/multipart/MultipartFile))
{
super.encode(object, bodyType, template);//调用FormEncoder对应方法
return;
} else
{
MultipartFile file = (MultipartFile)object;
java.util.Map data = Collections.singletonMap(file.getName(), object);
super.encode(data, MAP_STRING_WILDCARD, template);
return;
}
}
}

可以发现SpringFormEncoder的encode方法当传送的对象不是MultipartFile的时候,就会调用super.encode, 也就是FormEncoder的encode方法。

FormEncoder类的部分源码:

 public FormEncoder()
{
this(((Encoder) (new feign.codec.Encoder.Default())));
} public FormEncoder(Encoder delegate)
{
_flddelegate = delegate;
List list = Arrays.asList(new ContentProcessor[] {
new MultipartFormContentProcessor(delegate), new UrlencodedFormContentProcessor()
});
processors = new HashMap(list.size(), 1.0F);
ContentProcessor processor;
for(Iterator iterator = list.iterator(); iterator.hasNext(); processors.put(processor.getSupportedContentType(), processor))
processor = (ContentProcessor)iterator.next(); } public void encode(Object object, Type bodyType, RequestTemplate template)
throws EncodeException
{
String contentTypeValue = getContentTypeValue(template.headers());//这里会去到@PostMapping中consumes的值,所以参数需要传对象时指定一下consumes
ContentType contentType = ContentType.of(contentTypeValue);//为啥指定consumes,是因为不指定就是application/x-www-form-urlencoded,而且processors中也包含,为啥包含见FormEncoder的构造函数
if(!MAP_STRING_WILDCARD.equals(bodyType) || !processors.containsKey(contentType))
{
_flddelegate.encode(object, bodyType, template);//_flddelegate是啥呢,是SpringFormEncoder传递过来,也就是new Encoder.Default()
return;
}
Charset charset = getCharset(contentTypeValue);
Map data = (Map)object;
try
{
((ContentProcessor)processors.get(contentType)).process(template, charset, data);
}
catch(Exception ex)
{
throw new EncodeException(ex.getMessage());
}
}

FormEncoderr的encode方法当传送的对象是json格式的字符串的时候,就会调用 _flddelegate.encode,即Encoder.Default的encode方法,而这个Encoder.Default的encode方法判断传送的类型不是String或者byte[],就会抛异常

 public interface Encoder
{
public static class Default
implements Encoder
{ public void encode(Object object, Type bodyType, RequestTemplate template)
{
if(bodyType == java/lang/String)
template.body(object.toString());
else
if(bodyType == [B)
template.body((byte[])(byte[])object, null);
else
if(object != null)//当我们用对象传递参数的时候,会走这里
throw new EncodeException(String.format("%s is not a type supported by this encoder.", new Object[] {
object.getClass()
}));
} public Default()
{
}
} public abstract void encode(Object obj, Type type, RequestTemplate requesttemplate)
throws EncodeException; public static final Type MAP_STRING_WILDCARD = Util.MAP_STRING_WILDCARD; }

解决方案一:继续使用前面提到的方案,如果引用该配置类的FeignClient中,没有使用实体类作为参数的接口,则去掉配置类上的注解@Configuration就可以了,去掉注解@Configuration之后,该配置就只对通过configuration属性引用该配置的FeignClient起作用(或者将该文件上传接口单独放到一个FeignClient中,去掉配置类上的注解@Configuration)。

方案一只支持文件上传,如果引用该配置的FeignClient中有使用实体类作为参数接收的接口,则调用该接口时会抛异常。

解决方案二:继续使用前面提到的方案,将配置文件修改为如下:

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

方案二既支持文件上传也支持实体类作为参数接收。

spring cloud feign 上传文件报not a type supported by this encoder解决方案的更多相关文章

  1. RestTemplate OR Spring Cloud Feign 上传文件

    SpringBoot,通过RestTemplate 或者 Spring Cloud Feign,上传文件(支持多文件上传),服务端接口是MultipartFile接收. 将文件的字节流,放入ByteA ...

  2. Spring使用mutipartFile上传文件报错【Failed to instantiate [org.springframework.web.multipart.MultipartFile]】

    报错场景: 使用SSM框架实现文件上传时报“Failed to instantiate [org.springframework.web.multipart.MultipartFile]”错,控制器源 ...

  3. Spring MVC实现上传文件报错解决方案

    报错代码: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.sp ...

  4. spring mvc(注解)上传文件的简单例子

    spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...

  5. Linux - xshell上传文件报错乱码

    xshell上传文件报错乱码,解决方法 rz -be 回车 下载sz  filename

  6. Azkban上传文件报错installation Failed.Error chunking

    azkaban 上传文件报错Caused by: java.sql.SQLException: The size of BLOB/TEXT data inserted in one transacti ...

  7. Tomcat上传文件报错:returned a response status of 403 Forbidden

    出现这样的错误是没有权限对服务器进行写操作.需要在这个项目所在的tomcat中配置可写操作即可: 在tomcat的web.xml添加下面代码: <init-param><param- ...

  8. Bug集锦-Spring Cloud Feign调用其它接口报错

    问题描述 Spring Cloud Feign调用其它服务报错,错误提示如下:Failed to instantiate [java.util.List]: Specified class is an ...

  9. rz上传文件报错:rpm Read Signature failed: sigh blob(1268): BAD, read returned 0

    上传文件报错: [root@www localdisk]# rpm -ivh cobbler* error: cobbler-2.8.4-4.el7.x86_64.rpm: rpm  Read  Si ...

随机推荐

  1. ipconfig 命令有什么作用

    ipconfig /all:显示本机TCP/IP配置的详细信息:ipconfig /release:DHCP客户端手工释放IP地址:ipconfig /renew:DHCP客户端手工向服务器刷新请求: ...

  2. Android开发(七)——判断网络状态

    项目中难免会出现使用网络的情况,使用网络前得进行网络判断,看网上的网友一般有多种实现版本. 第一种: // 是否有网络连接 public static boolean isNetworkConnect ...

  3. C#学习笔记(30)——系统自带委托Func和Action

    说明(2017-11-23 10:46:33): 1. Func有返回值,Action无返回值,以后就不用定义delegate委托了. 2. 不过还是不知道什么时候该用委托,蒋坤在讲完事件后,留了个作 ...

  4. DeepNLP的核心关键/NLP词的表示方法类型/NLP语言模型 /词的分布式表示/word embedding/word2vec

    DeepNLP的核心关键/NLP语言模型 /word embedding/word2vec Indexing: 〇.序 一.DeepNLP的核心关键:语言表示(Representation) 二.NL ...

  5. postgresql with递归

    在PostgreSQL里,with子句提供了一种方法写一个大的查询中使用的辅助报表与查询.它有助于打破复杂和大型查询简单易读的形式. 1. 建表 postgres=# create table tb9 ...

  6. Android——Fragment+Editext总结

    原文地址: android Fragment中没有onTouchEvent解决方法 Android--点击EditText的时候弹出软键盘,点击EditText之外空白处软键盘消失,android-- ...

  7. [转]为什么Java中的HashMap默认加载因子是0.75

    前几天在一个群里看到有人讨论hashmap中的加载因子为什么是默认0.75. HashMap源码中的加载因子 static final float DEFAULT_LOAD_FACTOR = 0.75 ...

  8. 解决mysql开启GTID主从同步出现1236错误问题

    解决mysql开启GTID主从同步出现1236错误问题     最近遇到mysql开启gtid做复制时,从库出现1236错误,导致同步无法进行,本文就这问题记录下处理步骤,有关gtid知识在这里不做介 ...

  9. XXXXX

    http://www.java-gaming.org/topics/laser-fx-opengl-libgdx/25402/view.html Libgdx游戏引擎之Dialog组件 http:// ...

  10. PHP数组缓存:三种方式JSON、序列化和var_export的比较

    使用PHP的站点系统,在面对大数据量的时候不得不引入缓存机制.有一种简单有效的办法是将PHP的对象缓存到文件里.下面我来对这3种缓存方法进行说明和比较. 第一种方法:JSONJSON缓存变量的方式主要 ...