Spring webflux WebClient post 给客户端一个文件
Spring webflux WebClient post a file to a client
我正在尝试弄清楚如何编写一种方法来简单地将文件从 webflux 控制器发送到 'regular' 控制器。
我经常收到常见错误,但我尝试过的任何方法都没有解决它。
我发送文件的方法:
@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 网关上配置一些东西以允许请求参数进入?
这对我来说很有趣。当我在接收端使用 lambda 函数并使用 aws-serverless-java-container-spring
时,我实际上不得不手动声明 MultipartResolver
。
我添加
后问题中的代码可以正常工作
@Bean
public MultipartResolver multipartResolver() {
return new CommonsMultipartResolver();
}
到我的配置。
也许有人会无意中发现它并发现它很有用。
我正在尝试弄清楚如何编写一种方法来简单地将文件从 webflux 控制器发送到 'regular' 控制器。
我经常收到常见错误,但我尝试过的任何方法都没有解决它。
我发送文件的方法:
@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 网关上配置一些东西以允许请求参数进入?
这对我来说很有趣。当我在接收端使用 lambda 函数并使用 aws-serverless-java-container-spring
时,我实际上不得不手动声明 MultipartResolver
。
我添加
后问题中的代码可以正常工作@Bean
public MultipartResolver multipartResolver() {
return new CommonsMultipartResolver();
}
到我的配置。
也许有人会无意中发现它并发现它很有用。