Spring Web Reactive Framework多部分文件问题


问题内容

我正在尝试通过尝试以下操作来使用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));
    }
}

但是我不断收到以下错误消息415:

Response status 415 with reason "Content type 'multipart/form-data;boundary=--0b227e57d1a5ca41' not supported\

不知道是什么问题,我正在按以下方式卷曲API:

 curl -v -F "file=@jinyang.gif" -H "Content-Type: multipart/form-data" localhost:8080/images

我试过了头文件和文件具有相同结果的不同变体。在这里有点不知所措,因为我过去做过,事情似乎还可以。


问题答案:

深入研究之后,我可以在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));
    }
}