怎么在spring中使用 feign,解决上传文件的痛点

目录

笔者在长期使用第三方的http 的restful风格接口中,感觉到十分的麻烦,发现 在spring cloud 提供的组件中,
feign 是十分好用的,而且十分清晰的可以看到自己 调用的其他的系统的接口
但是 很明显在 大量的百度之后,我发现根本没有一个可以运行的方案,要么就是一堆代码,要么就是 运行不了,索性自己写

feign初始

在openfeign开源项目中,有一个 提供例子,稍加改变放在spring 项目中,证明是可以的,
重要的是先 导入包,用于 传入和传出的参数,可以自由选择 json序列化的包
Gson 和 jackson ,fastJson 都行

<dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-core</artifactId>
            <version>9.5.0</version>
        </dependency>
        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-gson</artifactId>
            <version>9.3.1</version>
        </dependency>
        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-jackson</artifactId>
            <version>9.5.0</version>
        </dependency>

调用的第三方的接口的service,这个方法 我刻意写的是 Map

 @Headers({"Content-Type: application/json","Accept: application/json"})
public interface GitHub {
    @RequestLine("POST /bonusPool/getone")
    Map<String,Object> repos(@RequestBody Map<String,Object> log);
}

书写用于spring 注入的配置类

该配置类可以自己扩展,比如什么超时时间,Header请求头,请求体的 编码,,,,这里 只给出一个基本的配置,baseUrl 是 自己在配置文件配置的,可以随着 不同的环境去变化,

@Configuration
@ConditionalOnClass({GitHub.class})
public class Configs {

    @Value("${application.cnode.url}")
    private String baseurtl;

    @Bean
    public GitHub getGit(){
      return Feign.builder()
              .decoder(new JacksonDecoder())
              .encoder(new JacksonEncoder())
              .logLevel(Logger.Level.FULL)
            /*  .requestInterceptor(new RequestInterceptor(){
                  @Override
                  public void apply(RequestTemplate requestTemplate) {

                     requestTemplate.header("Content-Type", "application/json");

                  }
              })*/
              .target(GitHub.class, baseurtl);
     }
}

具体使用

你可以自己定义vo,也可以图方便自己写Map


    @Autowired
    private GitHub gitHub;
    @Test
    public void  testFeign(){
        Map<String,Object> h=new HashMap<>();
        h.put("createTime","asdf");
        Map<String,Object> ss=gitHub.repos(h);
}

上传文件feign

笔者在 实际生产中,遇到需要上传文件的需求,基本上这个问题在百度是搜索不到的,所以艰难之下看到一个帖子,找到了老外的代码,亲测可用,
引入 spring-form-feign的包

 <dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form-spring</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>

然后将之前的
Feign.builder()
.decoder(new JacksonDecoder())
.encoder(new JacksonEncode())
的编码方式 进行 改变,
很明显 市面上没有这中 兼容 form-multifile 和普通的 encode 的类,只能自己写,

package com.jufan.install.feign;

import feign.RequestTemplate;
import feign.codec.EncodeException;
import feign.codec.Encoder;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author 参数编码的类,默认是 jacksonEncode,有文件就是 springformFeign
 * @create 2018-07-27 9:35
 **/
public class FeignSpringFormEncoder implements Encoder {


    private final List<HttpMessageConverter<?>> converters = new RestTemplate().getMessageConverters();
    private final HttpHeaders multipartHeaders = new HttpHeaders();
    private final HttpHeaders jsonHeaders = new HttpHeaders();

    public static final Charset UTF_8 = Charset.forName("UTF-8");

    public FeignSpringFormEncoder() {
        multipartHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
        jsonHeaders.setContentType(MediaType.APPLICATION_JSON);
    }

    /**
     * {@inheritDoc }
     */
    @Override
    public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException {
        if (isFormRequest(bodyType)) {
            encodeMultipartFormRequest((Map<String, ?>) object, template);
        } else {
            encodeRequest(object, jsonHeaders, template);
        }
    }

    /**
     * Encodes the request as a multipart form. It can detect a single {@link MultipartFile}, an
     * array of {@link MultipartFile}s, or POJOs (that are converted to JSON).
     *
     * @param formMap
     * @param template
     * @throws EncodeException
     */
    private void encodeMultipartFormRequest(Map<String, ?> formMap, RequestTemplate template) throws EncodeException {
        if (formMap == null) {
            throw new EncodeException("Cannot encode request with null form.");
        }
        LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        for (Entry<String, ?> entry : formMap.entrySet()) {
            Object value = entry.getValue();
            if (isMultipartFile(value)) {
                map.add(entry.getKey(), encodeMultipartFile((MultipartFile) value));
            } else if (isMultipartFileArray(value)) {
                encodeMultipartFiles(map, entry.getKey(), Arrays.asList((MultipartFile[]) value));
            } else {
                map.add(entry.getKey(), encodeJsonObject(value));
            }
        }
        encodeRequest(map, multipartHeaders, template);
    }

    private boolean isMultipartFile(Object object) {
        return object instanceof MultipartFile;
    }

    private boolean isMultipartFileArray(Object o) {
        return o != null && o.getClass().isArray() && MultipartFile.class.isAssignableFrom(o.getClass().getComponentType());
    }

    /**
     * Wraps a single {@link MultipartFile} into a {@link HttpEntity} and sets the
     * {@code Content-type} header to {@code application/octet-stream}
     *
     * @param file
     * @return
     */
    private HttpEntity<?> encodeMultipartFile(MultipartFile file) {
        HttpHeaders filePartHeaders = new HttpHeaders();
        filePartHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        try {
            Resource multipartFileResource = new MultipartFileResource(file.getOriginalFilename(), file.getSize(), file.getInputStream());
            return new HttpEntity<>(multipartFileResource, filePartHeaders);
        } catch (IOException ex) {
            throw new EncodeException("Cannot encode request.", ex);
        }
    }

    /**
     * Fills the request map with {@link HttpEntity}s containing the given {@link MultipartFile}s.
     * Sets the {@code Content-type} header to {@code application/octet-stream} for each file.
     *
     * @param name  the name of the array field in the multipart form.
     * @param files
     */
    private void encodeMultipartFiles(LinkedMultiValueMap<String, Object> map, String name, List<? extends MultipartFile> files) {
        HttpHeaders filePartHeaders = new HttpHeaders();
        filePartHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        try {
            for (MultipartFile file : files) {
                Resource multipartFileResource = new MultipartFileResource(file.getOriginalFilename(), file.getSize(), file.getInputStream());
                map.add(name, new HttpEntity<>(multipartFileResource, filePartHeaders));
            }
        } catch (IOException ex) {
            throw new EncodeException("Cannot encode request.", ex);
        }
    }

    /**
     * Wraps an object into a {@link HttpEntity} and sets the {@code Content-type} header to
     * {@code application/json}
     *
     * @param o
     * @return
     */
    private HttpEntity<?> encodeJsonObject(Object o) {
        HttpHeaders jsonPartHeaders = new HttpHeaders();
        jsonPartHeaders.setContentType(MediaType.APPLICATION_JSON);
        return new HttpEntity<>(o, jsonPartHeaders);
    }

    /**
     * Calls the conversion chain actually used by
     * {@link org.springframework.web.client.RestTemplate}, filling the body of the request
     * template.
     *
     * @param value
     * @param requestHeaders
     * @param template
     * @throws EncodeException
     */
    private void encodeRequest(Object value, HttpHeaders requestHeaders, RequestTemplate template) throws EncodeException {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        HttpOutputMessage dummyRequest = new HttpOutputMessageImpl(outputStream, requestHeaders);
        try {
            Class<?> requestType = value.getClass();
            MediaType requestContentType = requestHeaders.getContentType();
            for (HttpMessageConverter<?> messageConverter : converters) {
                if (messageConverter.canWrite(requestType, requestContentType)) {
                    ((HttpMessageConverter<Object>) messageConverter).write(
                            value, requestContentType, dummyRequest);
                    break;
                }
            }
        } catch (IOException ex) {
            throw new EncodeException("Cannot encode request.", ex);
        }
        HttpHeaders headers = dummyRequest.getHeaders();
        if (headers != null) {
            for (Entry<String, List<String>> entry : headers.entrySet()) {
                template.header(entry.getKey(), entry.getValue());
            }
        }
        /*
        we should use a template output stream... this will cause issues if files are too big,
        since the whole request will be in memory.
         */
        template.body(outputStream.toByteArray(), UTF_8);
    }

    /**
     * Minimal implementation of {@link org.springframework.http.HttpOutputMessage}. It's needed to
     * provide the request body output stream to
     * {@link org.springframework.http.converter.HttpMessageConverter}s
     */
    private class HttpOutputMessageImpl implements HttpOutputMessage {

        private final OutputStream body;
        private final HttpHeaders headers;

        public HttpOutputMessageImpl(OutputStream body, HttpHeaders headers) {
            this.body = body;
            this.headers = headers;
        }

        @Override
        public OutputStream getBody() throws IOException {
            return body;
        }

        @Override
        public HttpHeaders getHeaders() {
            return headers;
        }

    }

    /**
     * Heuristic check for multipart requests.
     *
     * @param type
     * @return
     */
    static boolean isFormRequest(Type type) {
        return MAP_STRING_WILDCARD.equals(type);
    }

    /**
     * Dummy resource class. Wraps file content and its original name.
     */
    static class MultipartFileResource extends InputStreamResource {

        private final String filename;
        private final long size;

        public MultipartFileResource(String filename, long size, InputStream inputStream) {
            super(inputStream);
            this.size = size;
            this.filename = filename;
        }

        @Override
        public String getFilename() {
            return this.filename;
        }

        @Override
        public InputStream getInputStream() throws IOException, IllegalStateException {
            return super.getInputStream(); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public long contentLength() throws IOException {
            return size;
        }

    }

}

于是配置类稍微改动就行

@Configuration
@ConditionalOnClass({RiskwebService.class})
public class RiskwebServiceConfig {


    @Value("${rmps.prefix.url}")
    private String riskWebPrefix;

    @Bean
    public RiskwebService ConstructionRiskwebService() {
        return Feign.builder()
                .decoder(new JacksonDecoder())
                .encoder(new FeignSpringFormEncoder())
                .logger(new Logger.ErrorLogger())
                .logLevel(Logger.Level.BASIC)
                .errorDecoder(new RiskErrorDecoder(new JacksonDecoder()))
                .target(RiskwebService.class, riskWebPrefix);
    }

}

使用spring-form

使用时 就是 用 @Param 进行 替换,


    /**
     * 上传文件
     *
     * @param file
     * @param outUserId
     * @return
     * @PostMapping("/upload/test/images") public JSONObject getFile(@RequestParam("file") MultipartFile multipartFile,
     * @RequestParam(value = "outUserId") String outUserId)throws Exception {
     * return riskwebService.fileUpload(multipartFile,outUserId);
     * }
     */
    @RequestLine("POST bonusPool/getfile")
    JSONObject fileUpload(@Param("file") MultipartFile file, @Param("outUserId") String outUserId);

controller层使用

 @PostMapping("/upload/test/images")
    public JSONObject getFile(@RequestParam("file") MultipartFile multipartFile,
                              @RequestParam(value = "outUserId") String outUserId) throws Exception {

        return riskwebService.fileUpload(multipartFile, outUserId);

    }

猜你喜欢

转载自blog.csdn.net/sinat_27639721/article/details/81218357
今日推荐