SpringFormEncoder.class(When bodyType is MultipartFile[].class,files should not be override)
it happens line 67-69 in the source code of SpringFormEncoder.class, because each file has same file.getName().. **``` if (bodyType.equals(MultipartFile[].class)) { val files = (MultipartFile[]) object; val data = new HashMap<String, Object>(files.length, 1.F); for (val file : files) { data.put(file.getName(), file); } super.encode(data, MAP_STRING_WILDCARD, template); }
Hello, do you have a solution? I have encountered the same problem.
您好,您有解决方案吗?我遇到了同样的问题。
您好,您有解决方案吗?我遇到了同样的问题。
Hello, do you have a solution? I have encountered the same problem.
yeah, you can use
Map data = Collections.singletonMap(file.length == 0 ? "" : file[0].getName(), object);
instead foreach
您能给出具体的解决方案吗?十分感谢。
您能给出具体的解决方案吗?十分感谢。
可以暂时自己自定义一个SpringFormEncoder,等后续官方解决了再说。 `package com.hb56.block.config.feign;
import feign.codec.Encoder; import org.springframework.beans.factory.ObjectFactory; import org.springframework.boot.autoconfigure.http.HttpMessageConverters; import org.springframework.cloud.openfeign.support.SpringEncoder; import org.springframework.context.annotation.Bean;
/**
-
@author Been
-
@description feign-form局部configuration用于表单提交和上传文件 */ public class FeignFormConfig { private final ObjectFactory<HttpMessageConverters> messageConverters;
public FeignFormConfig(ObjectFactory<HttpMessageConverters> messageConverters) { this.messageConverters = messageConverters; } // /** // * 自定义Charset // * @param messageConverters // */ // public FeignFormConfig(ObjectFactory<HttpMessageConverters> messageConverters) { // List<HttpMessageConverter>> list = messageConverters.getObject().getConverters(); // for (HttpMessageConverter> httpMessageConverter : list) { // if (httpMessageConverter instanceof StringHttpMessageConverter) { // ((StringHttpMessageConverter) httpMessageConverter).setDefaultCharset(Charset.forName("GBK")); // break; // } // } // this.messageConverters = messageConverters; // }
@Bean public Encoder multipartFormEncoder() { return new SpringMultipartEncoder(new SpringEncoder(messageConverters)); }
} ` 其中的SpringMultipartEncoder可以参考SpringFormEncoder
十分感谢,问题已经解决了,之前我改了那个foreach,不是用map接的,用的val,然后调用报错了。 val data = Collections.singletonMap(file.length == 0 ? "" : file[0].getName(), object);
其实这个问题有人已经贡献改进源码了,可能下个版本就修复了,你也可以参照它使用的LinkedMultiValueMap来操作。 https://github.com/OpenFeign/feign-form/pull/74/commits/4131ead3593e81de87f8bfa62e730af58a8ee8e2