问题描述
我正在尝试通过以下方式使用 Spring 的 Reactive Framework 实现和图像上传:
I'm trying to implement and image upload using Spring's Reactive Framework by trying the following:
@RestController
@RequestMapping("/images")
public class ImageController {
@Autowired
private IImageService imageService;
@PostMapping(value = "", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
Mono<ImageEntity> saveImage(@RequestBody Mono<FilePart> part) throws Exception{
return part.flatMap(file -> imageService.saveImage(file));
}
}
但我不断收到带有以下错误消息的 415:
But I keep getting a 415 with the following error message:
Response status 415 with reason "Content type 'multipart/form-data;boundary=--0b227e57d1a5ca41' not supported\
不确定是什么问题,我按以下方式卷曲 API:
Not sure what the issue is, I'm curling the API the following way:
curl -v -F "file=@jinyang.gif" -H "Content-Type: multipart/form-data" localhost:8080/images
我尝试了不同的标头和文件变体,结果相同.在这里有点不知所措,因为我过去做过这件事,而且一切似乎都很好.我从这篇文章中看到此功能已合并:
I've tried different variations of headers and files with the same results. Kind of at a loss here because I've done this in the past and things seemed to work okay. I saw from this post that this feature was merged:
如何启用 Spring Reactive Web MVC处理多部分文件?
推荐答案
经过深入研究,我在 Spring WebFlux 项目中找到了这个测试:
After digging around I was able to find this test in the Spring WebFlux project:
所以我缺少的部分是控制器定义中的 @RequestPart
而不是 @RequestBody
.
So the part I was missing was @RequestPart
instead of @RequestBody
in the controller definition.
最终代码如下所示:
@RestController
@RequestMapping("/images")
public class ImageController {
@Autowired
private IImageService imageService;
@PostMapping(value = "", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
Mono<ImageEntity> saveImage(@RequestPart("file") Mono<FilePart> part) throws Exception{
return part.flatMap(file -> imageService.saveImage(file));
}
}
这篇关于Spring Web Reactive Framework 多部分文件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!