其他分享
首页 > 其他分享> > Feign 调用 带有上传图片接口 导致服务点 调用失败 解决方案

Feign 调用 带有上传图片接口 导致服务点 调用失败 解决方案

作者:互联网

创建 FeignConfig

点击查看代码
@Configuration
public class FeignConfig {
    @Bean
    public Decoder customErrorDecoder() {
        return new FeignClientDecoder();
    }
    @Autowired
    private ObjectFactory<HttpMessageConverters> messageConverters;
    @Bean
    @Primary
    @Scope("prototype")
    public Encoder multipartFormEncoder() {
        return new FeignSpringFormEncoder(new SpringEncoder(messageConverters));
    }

}

创建方法FeignSpringFormEncoder

点击查看代码
package com.zhuzaocloud.vcs.app.config;

import feign.RequestTemplate;
import feign.codec.EncodeException;
import feign.codec.Encoder;
import feign.form.ContentType;
import feign.form.FormEncoder;
import feign.form.MultipartFormContentProcessor;
import feign.form.spring.SpringManyMultipartFilesWriter;
import feign.form.spring.SpringSingleMultipartFileWriter;
import org.springframework.web.multipart.MultipartFile;

import java.lang.reflect.Type;
import java.util.*;

public class FeignSpringFormEncoder extends FormEncoder {
    public FeignSpringFormEncoder() {
        this(new Default());
    }
    public FeignSpringFormEncoder(Encoder delegate) {
        super(delegate);
        MultipartFormContentProcessor processor = (MultipartFormContentProcessor) this.getContentProcessor(ContentType.MULTIPART);
        processor.addFirstWriter(new SpringManyMultipartFilesWriter());
        processor.addFirstWriter(new SpringSingleMultipartFileWriter());
    }
    @Override
    public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException {
        //修改为下面的代码
        if (bodyType.equals(MultipartFile.class)) {
            MultipartFile file = (MultipartFile) object;
            Map<String, Object> 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<String, Object> data = Collections.singletonMap(file.length == 0 ? "" : file[0].getName(), object);
                super.encode(data, MAP_STRING_WILDCARD, template);
                Map<String, Collection<String>> headers = template.headers();
                Collection<String> strings = headers.get("Content-Type");
                Collection<String> a = new ArrayList<>();
                strings.stream().forEach(x -> {
                    if (x.startsWith("multipart/form-data; charset=UTF-8; boundary=")) {
                        a.add(x);
                    }
                });
                Map<String, Collection<String>> res = new TreeMap<>();
                template.headers(res);
                res.put("Content-Length", headers.get("Content-Length"));
                res.put("Content-Type", a);
                template.headers(res);
                return;
            }
        }
        super.encode(object, bodyType, template);
    }
}

标签:Feign,调用,feign,template,new,import,MultipartFile,上传,public
来源: https://www.cnblogs.com/zxy-come-on/p/16390891.html