Spring Web Reactive Framework 多部分文件问题

标签 spring spring-mvc reactive project-reactor

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

不确定问题是什么,我正在通过以下方式 curl API:
 curl -v -F "file=@jinyang.gif" -H "Content-Type: multipart/form-data" localhost:8080/images

我尝试了不同的 header 和文件变体,结果相同。在这里有点不知所措,因为我过去做过这件事,而且一切似乎都很好。我从这篇文章中看到此功能已合并:

How to enable Spring Reactive Web MVC to handle Multipart-file?

最佳答案

在挖掘之后,我能够在 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在 Controller 定义中。

最终代码如下所示:

@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 多部分文件问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47703924/

相关文章:

java - 使用 WebClient/Spring Boot 2 将 REST 响应映射到 Mono<SomeClass> 的正确方法

spring - 使用 Hibernate 和 Spring MVC 的 JSP

java - 使用 Spring Boot 在两个或多个 (Oracle) 数据库上使用多个存储过程?

java - 我应该用一个字段创建 Spring 模型类吗?

java - RequestMappingHandlerMapping 是 RestController 的截断路径

spring-webflux - ReactiveQuerydslPredicateExecutor 用于 Reactive WebFlux 的分页

spring - 自定义实现UserDetails的示例

eclipse - 使用 Eclipse Tomcat Maven M2Eclipse 的 ClassNotFound W/Spring

javascript - 使用jQuery在jsp中动态加载spring复选框标签时出错

c# - 是否可以向 Rx.Net Timeout 运算符添加自定义消息