spring cloud feign上传文件配置

package com.github.pig.common.bean.feign;


import com.github.pig.common.bean.feign.fallback.FileServiceFallbackImpl;
import com.github.pig.common.vo.FileResult;
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.cloud.netflix.feign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

@FeignClient(name = "pig-fileserver", fallback = FileServiceFallbackImpl.class,configuration = FileService.MultipartSupportConfig.class)
public interface FileService {

    /**
     * 文件上传
     * @param file
     * @return
     */
    @PostMapping(value = "/", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    FileResult upload(@RequestPart("file") MultipartFile file);

    /**
     * 删除文件
     * @param filePath
     */
    @DeleteMapping
    FileResult delete(@RequestParam("filePath") String filePath);

    @Configuration
    class MultipartSupportConfig {
        @Autowired
        private ObjectFactory messageConverters;
        @Bean
        public Encoder feignFormEncoder() {
            return new SpringFormEncoder(new SpringEncoder(messageConverters));
        }
    }
}

你可能感兴趣的:(spring cloud feign上传文件配置)