代码之家  ›  专栏  ›  技术社区  ›  Chris

Spring webflux WebClient将文件发布到客户端

  •  0
  • Chris  · 技术社区  · 7 年前

    我经常会发现一个常见的错误,但我没有尝试过解决它。

    @GetMapping("process")
    public Flux<String> process() throws MalformedURLException {
        final UrlResource resource = new UrlResource("file:/tmp/document.pdf");
    
        MultiValueMap<String, UrlResource> data = new LinkedMultiValueMap<>();
        data.add("file", resource);
    
        return webClient.post()
                .uri(LAMBDA_ENDPOINT)
                .contentType(MediaType.MULTIPART_FORM_DATA)
                .body(BodyInserters.fromMultipartData(data))
                .exchange()
                .flatMap(response -> response.bodyToMono(String.class))
                .flux();
    }
    

    我正在AWS Lambda中使用它,具有以下端点:

        @PostMapping(path = "/input", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
        public ResponseEntity<List<?>> input(@RequestParam("file") MultipartFile file) throws IOException {
            final ByteBuffer byteBuffer = ByteBuffer.wrap(file.getBytes());
    
            //[..]
    
            return new ResponseEntity<>(result, HttpStatus.OK);
        }
    

    但我一直在想:

    {  
       "timestamp":1549395273838,
       "status":400,
       "error":"Bad Request",
       "message":"Required request part 'file' is not present",
       "path":"/detect-face"
    }
    

    从lambda回来;

    我只是错误地设置了文件的发送,还是需要在API网关上配置一些东西以允许请求参数进入?

    1 回复  |  直到 7 年前
        1
  •  2
  •   Chris    7 年前

    aws-serverless-java-container-spring 事实上,我必须申报 MultipartResolver 手动。

    @Bean
    public MultipartResolver multipartResolver() {
        return new CommonsMultipartResolver();
    }
    

    到我的配置。