fegin文件上传远程调用配置

fegin文件上传远程调用配置,第1张

fegin文件上传远程调用配置

springcloud中fegin不支持直接传文件,可通过引入fegin的扩展包来实现

1、在消费方引入fegin对表单提交的依赖


   io.github.openfeign.form
   feign-form
   3.0.3


   io.github.openfeign.form
   feign-form-spring
   3.0.3


   commons-fileupload
   commons-fileupload
   1.3.3

2、在服务提供方编写上传接口

@PostMapping(value = "/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String handleFileUpload(@RequestPart(value = "file") MultipartFile file) {
        return file.getOriginalFilename();
    }

3、在服务消费方编写文件传输编码配置项

   @Configuration
    class MultipartSupportConfig {
        @Bean
        public Encoder feignFormEncoder() {
            return new SpringFormEncoder();
        }
    }

4、在消费端编写文件上传接口

@FeignClient("eureka-client")
public interface DcClient {

    @GetMapping("/dc")
    String consumer();

    @PostMapping(value = "/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    String handleFileUpload(@RequestPart(value = "file") MultipartFile file);
}

@RestController
public class DcController {

//    @Autowired
//    LoadBalancerClient loadBalancerClient;

//    @Autowired
//    RestTemplate restTemplate;

    @Autowired
    DcClient dcClient;


//    @GetMapping("/consumer")
//    public String dc() {
        ServiceInstance serviceInstance = loadBalancerClient.choose("eureka-client");
        String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/dc";
        System.out.println(url);
//        return restTemplate.getForObject("http://eureka-client/dc", String.class);
//    }


    @GetMapping("/consumer")
    public String dc() {
        return dcClient.consumer();
    }

    @PostMapping(value = "/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String handleFileUpload(@RequestPart(value = "file") MultipartFile file){
        return dcClient.handleFileUpload(file);
    }

5、postman调用文件上传

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5608186.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存