美文网首页
feign.codec.EncodeException: i

feign.codec.EncodeException: i

作者: 莫夏_b560 | 来源:发表于2019-12-19 19:02 被阅读0次

    默认应该使用SpringEncoder才对,但可能你在项目中指定了其他的Encoder

    @SpringBootApplication
    @EnableCircuitBreaker
    @EnableEurekaClient
    @EnableFeignClients
    @ComponentScan(excludeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,value = FeignMultipartSupportConfig.class)})
    public class BootstrapApplication {
     
       public static void main(String[] args) {
          SpringApplication.run(BootstrapApplication.class, args);
       }
    }
    

    在目标feign上面添加:

    @FeignClient(name = "micro-picture", fallbackFactory = MicroPictureFactory.class, configuration = FeignMultipartSupportConfig.class)
    public interface MicroPictureClient {
    
    @RequestMapping(value = { "/picture/common/upload/{commonKey}" }, method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
       public String upload(@RequestPart("image") MultipartFile image, @PathVariable("commonKey") Long commonKey);
    }
    

    就可以实现对应的服务做图片的上传,针对的图片微服务就可以实现数据的额接收。

    对应配置文件的代码:

    @Configuration
    public class FeignMultipartSupportConfig {
    
       @Bean
       @Primary
       @Scope("prototype")
       public Encoder multipartFormEncoder() {
          return new FeignSpringFormEncoder();
       }
    
       @Bean
       public feign.Logger.Level multipartLoggerLevel() {
          return feign.Logger.Level.FULL;
       }
    }
     
    
    package com.zhht.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.Collections;
    import java.util.Map;
    
    public class FeignSpringFormEncoder extends FormEncoder {
       
       public FeignSpringFormEncoder() {
          this(new Default());
       }
    
       public FeignSpringFormEncoder(Encoder delegate) {
          super(delegate);
          MultipartFormContentProcessor processor = (MultipartFormContentProcessor) this
                .getContentProcessor(ContentType.MULTIPART);
          processor.addWriter(new SpringSingleMultipartFileWriter());
          processor.addWriter(new SpringManyMultipartFilesWriter());
       }
    
       public void encode(Object object, Type bodyType, RequestTemplate template) throws EncodeException {
          if (bodyType.equals(MultipartFile.class)) {
             MultipartFile file = (MultipartFile) object;
             if (file != null) {
                Map<String, Object> data = Collections.singletonMap("image", 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("imgList", object);
                super.encode(data, MAP_STRING_WILDCARD, template);
                return;
             }
          }
          super.encode(object, bodyType, template);
       }
    }
    

    相关文章

      网友评论

          本文标题:feign.codec.EncodeException: i

          本文链接:https://www.haomeiwen.com/subject/rervnctx.html