spring cloud feign 上传文件报not a type supported by this encoder解决方案

上传文件调用外部服务报错: 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));
    }
 }

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


原文:https://www.cnblogs.com/UniqueColor/p/9647776.html
作者: UniqueColor