Feign httpclient文件上傳問題記錄

Feign httpclient文件上傳問題記錄

問題說明

原先項目http請求經過feign + ribbon + urlconnection 完成,考慮urlconnection頻繁鏈接釋放帶來網絡及cpu開銷問題採用http client做爲鏈接池,升級完後發現發現出現調用下游服務亂碼、傳文件 文件內容被篡改問題java

處理

亂碼問題

原先系統feign-core、feign-httpclient使用9.5.0 ,按GitHub issue看,這個版本並非使用utf-8做爲編碼,致使中文亂碼狀況,鑑於項目用的版本相對較老,這邊只升級到9.6.0解決了亂碼問題,以前也有經過feign攔截器處理,可是這樣須要判斷特定的content-type進行處理,避免麻煩,open-feign強調他們的編碼是utf-8,部分版本不是,能夠定義爲bug,既然是bug,能經過升級解決問題就直接升級了git

傳文件內容被篡改

有問題找GitHub,仍是因爲版本問題,老版本的一個bug,經過httpclient致使文件上傳內容篡改,3.0.3版本修復了,早期項目使用的版本是2.0.1,有點年頭,參考了下官方建議要求github

Requirements

The feign-form extension depend on OpenFeign and its concrete versions:spring

  • all feign-form releases before 3.5.0 works with OpenFeign 9.* versions;
  • starting from feign-form's version 3.5.0, the module works with OpenFeign 10.1.0 versions and greater.

IMPORTANT: there is no backward compatibility and no any gurantee that the feign-form's versions after 3.5.0work with OpenFeign before 10.*. OpenFeign was refactored in 10th release, so the best approach - use the freshest OpenFeign and feign-form versions.網絡

Notes:app

  • spring-cloud-openfeign uses OpenFeign 9.* till v2.0.3.RELEASE and uses 10.* after. Anyway, the dependency already has suitable feign-form version, see dependency pom, so you don't need to specify it separately;
  • spring-cloud-starter-feign is a deprecated dependency and it always uses the OpenFeign's 9.* versions.

使用了以下版本pom,接口httpclient下文件上傳內容被篡改問題ide

feign-version = 9.6.0
<dependency>
    <groupId>io.github.openfeign.form</groupId>
    <artifactId>feign-form</artifactId>
    <version>3.4.1</version>
</dependency>
<dependency>
    <groupId>io.github.openfeign.form</groupId>
    <artifactId>feign-form-spring</artifactId>
    <version>3.4.1</version>
</dependency>
<dependency>
			<groupId>io.github.openfeign</groupId>
			<artifactId>feign-httpclient</artifactId>
			<version>${feign.version}</version>
		</dependency>
		<dependency>
			<groupId>io.github.openfeign</groupId>
			<artifactId>feign-core</artifactId>
			<version>${feign.version}</version>
		</dependency>
		<dependency>
			<groupId>io.github.openfeign</groupId>
			<artifactId>feign-hystrix</artifactId>
			<version>${feign.version}</version>
		</dependency>
		<dependency>
			<groupId>io.github.openfeign</groupId>
			<artifactId>feign-slf4j</artifactId>
			<version>${feign.version}</version>
		</dependency>

多附件文件上傳問題

默認SpringFormEncoder只處理單文件,一個強轉就完事了ui

@Override
public void encode (Object object, Type bodyType, RequestTemplate template) throws EncodeException {
  if (!bodyType.equals(MultipartFile.class)) {
    super.encode(object, bodyType, template);
    return;
  }

  val file = (MultipartFile) object;
  val data = singletonMap(file.getName(), object);
  super.encode(data, MAP_STRING_WILDCARD, template);
}

咱們系統文件上傳支持多附件,這個須要重寫下this

/**
 * @author yugj
 * @date 2019/8/28 下午4:27.
 */
public class FeignSpringFormEncoder extends FormEncoder {


    /**
     * Constructor with the default Feign's encoder as a delegate.
     */
    public FeignSpringFormEncoder() {
        this(new Default());
    }


    /**
     * Constructor with specified delegate encoder.
     *
     * @param delegate delegate encoder, if this encoder couldn't encode object.
     */
    public FeignSpringFormEncoder(Encoder delegate) {
        super(delegate);

        MultipartFormContentProcessor processor = (MultipartFormContentProcessor) getContentProcessor(ContentType.MULTIPART);
        processor.addWriter(new SpringSingleMultipartFileWriter());
        processor.addWriter(new SpringManyMultipartFilesWriter());
    }


    @Override
    public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException {
        if (bodyType.equals(MultipartFile.class)) {
            MultipartFile file = (MultipartFile) object;
            Map data = Collections.singletonMap(file.getName(), object);
            super.encode(data, MAP_STRING_WILDCARD, template);
            return;
        } else if (bodyType.equals(MultipartFile[].class)) {
            MultipartFile[] file = (MultipartFile[]) object;
            if(file != null) {
                Map data = Collections.singletonMap(file.length == 0 ? "" : "file", object);
                super.encode(data, MAP_STRING_WILDCARD, template);
                return;
            }
        }
        super.encode(object, bodyType, template);
    }
}

對應feignclient使用這個配置便可編碼

@Service
@FeignClient(name = "xx-file-sv",
        fallbackFactory = FileGeneralHystrixFallbackAdapter.class,
        configuration = FeignSpringFormEncoder.class)
public interface IFileClient extends IFile {

}
相關文章
相關標籤/搜索