【问题标题】:Spring boot Forwarding a received MultiPartFile to Feign ClientSpring boot 将接收到的 MultiPartFile 转发到 Feign Client
【发布时间】:2020-03-17 12:10:01
【问题描述】:

是否可以将收到的 MultiPartFile 项从 REST 资源转发到 Feign 客户端并期望它工作?

我的情况是,我需要通过我的微服务中的 API 网关从客户端获取 MultiPartFile 请求,然后将其传递给外部服务,并在从外部服务获得响应后需要在主中间微服务并响应客户端。

微服务 REST API:

@RestController
@RequestMapping("/api")
public class VisionApiTestResource {

    ....

    @PostMapping("/vision-test/upload-image")
    public ResponseEntity<VisionApiTestResultDTO> handleFileUpload(@RequestParam("file") MultipartFile file ) {
        VisionApiTestResultDTO visionApiTestResultDTO = visionApiTestService.testVisionApi(file);
        return ResponseEntity.ok().body(visionApiTestResultDTO);
    }

    ....

}

调用Feign客户端的服务


@Service
public class VisionApiTestService {

    ...

    public VisionApiTestResultDTO testVisionApi(MultipartFile multipartFile) {

        BackgroundRemoverResultDTO backgroundRemoverResultDTO = backgroundRemoverClient.removeBackground(multipartFile);

        ...
    }

}

我的假客户:

@FeignClient(name = "BackgroundRemover", url = "http://localhost:8000")
public interface BackgroundRemoverClient {

    @RequestMapping(value = "/background_removal/remove_background/", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    BackgroundRemoverResultDTO removeBackground(@RequestPart("file") MultipartFile untouchedImageFile);

}

这是我得到的例外:

java.lang.IllegalArgumentException: Illegal base64 character 3a
        at java.util.Base64$Decoder.decode0(Base64.java:714)
        at java.util.Base64$Decoder.decode(Base64.java:526)
        at java.util.Base64$Decoder.decode(Base64.java:549)
        at com.whereandshare.kiosk.kioskservice.service.VisionApiTestService.testVisionApi(VisionApiTestService.java:37)
        at com.whereandshare.kiosk.kioskservice.service.VisionApiTestService$$FastClassBySpringCGLIB$$193667fb.invoke(<generated>)
        at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
        at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:769)

【问题讨论】:

    标签: spring multipart spring-cloud-feign feign


    【解决方案1】:

    实际上,通过 feign 客户端转发您从 spring boot REST 端点收到的 MultiPartFile 是没有问题的。

    而且打印出来的stacktrace跟feign和spring没有关系。这是我从 feign 取回结果后犯的一个小错误。

    【讨论】:

      猜你喜欢
      • 2019-04-17
      • 2017-09-22
      • 2018-06-14
      • 2022-12-29
      • 2021-02-09
      • 1970-01-01
      • 2017-11-27
      • 2019-03-05
      • 2018-06-04
      相关资源
      最近更新 更多