Spring Web Reactive Framework多部分文件问题

Joe*_*mes 7 spring spring-mvc project-reactor reactive

我试图通过尝试以下方法使用Spring的Reactive Framework实现和映像上传:

@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));
    }
}
Run Code Online (Sandbox Code Playgroud)

但我一直收到415,出现以下错误信息:

Response status 415 with reason "Content type 'multipart/form-data;boundary=--0b227e57d1a5ca41' not supported\
Run Code Online (Sandbox Code Playgroud)

不确定是什么问题,我正在通过以下方式卷曲API:

 curl -v -F "file=@jinyang.gif" -H "Content-Type: multipart/form-data" localhost:8080/images
Run Code Online (Sandbox Code Playgroud)

我尝试过不同的标题和文件变体,结果相同.这里有点不知所措,因为我过去做过这件事情似乎工作正常.我在这篇文章中看到这个功能被合并了:

如何启用Spring Reactive Web MVC来处理Multipart文件?

Joe*_*mes 5

深入研究之后,我可以在Spring WebFlux项目中找到此测试:

https://github.com/spring-projects/spring-framework/blob/master/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/MultipartIntegrationTests.java

因此,部分我缺少的是@RequestPart,而不是@RequestBody在控制器中定义。

最终代码如下所示:

@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));
    }
}
Run Code Online (Sandbox Code Playgroud)