OpenFeign / feign-form

Open Feign form encoder
Apache License 2.0
291 stars 81 forks source link

SpringFormEncoder.class(When bodyType is MultipartFile[].class,files should not be override) #78

Open Been24 opened 5 years ago

Been24 commented 5 years ago

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

y531110728 commented 5 years ago

Hello, do you have a solution? I have encountered the same problem.

Been24 commented 5 years ago

您好,您有解决方案吗?我遇到了同样的问题。

您好,您有解决方案吗?我遇到了同样的问题。

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

y531110728 commented 5 years ago

您能给出具体的解决方案吗?十分感谢。

Been24 commented 5 years ago

您能给出具体的解决方案吗?十分感谢。

可以暂时自己自定义一个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;

/**

} ` 其中的SpringMultipartEncoder可以参考SpringFormEncoder

y531110728 commented 5 years ago

十分感谢,问题已经解决了,之前我改了那个foreach,不是用map接的,用的val,然后调用报错了。 val data = Collections.singletonMap(file.length == 0 ? "" : file[0].getName(), object);

Been24 commented 5 years ago

其实这个问题有人已经贡献改进源码了,可能下个版本就修复了,你也可以参照它使用的LinkedMultiValueMap来操作。 https://github.com/OpenFeign/feign-form/pull/74/commits/4131ead3593e81de87f8bfa62e730af58a8ee8e2