【问题标题】:File upload in spring webflux - Required MultipartFile parameter 'file' is not presentspring webflux中的文件上传-不存在所需的MultipartFile参数'file'
【发布时间】:2019-03-14 03:21:30
【问题描述】:

我正在尝试使用 Spring Webflux 上传文件,但收到错误 Required MultipartFile parameter 'file' is not present

@RestController
@RequestMapping("/documents")
class MyController(val myService: MyService) {

    @PostMapping
    fun create(@RequestParam("file") file: MultipartFile): Mono<ResponseEntity<Map<String, String>>> {
        return myService.create()
    }
}

我也尝试将@RequestParam("file") file: MultipartFile 替换为ServerRequeset,但出现错误:

“无法解析公共 reactor.core.publisher.Mono 上类型 'org.springframework.web.reactive.function.server.ServerRequest' 的参数 0>> co.example.controllers.MyController.create(org.springframework .web.reactive.function.server.ServerRequest)"

【问题讨论】:

  • 如果是多部分请求,这里应该使用@RequestPart。另外,你有 Synchronoss NIO Multipart 库作为依赖项吗?
  • 哦,当我使用@RequestPart 时,处理程序方法现在被击中:) 现在它只是说不支持格式 pdf。我没有在 build.gradle 中声明明确的 nio 多部分依赖
  • 我在哪里添加了@RequestPart?你能发布一个答案吗?
  • @Clement 你是如何解决不支持 pdf 的问题的 :)
  • @GOXR3PLUS 为没有早点这样做的疏忽而道歉。我将在下面发布答案。

标签: spring kotlin spring-webflux


【解决方案1】:

MultipartFile 更改为 FilePart 最终对我有用 :)

@RestController
@RequestMapping("/v1/uploads")
class UploadsController(val exampleService: ExampleService) {

    @PostMapping(consumes = ["multipart/form-data"])
    fun create(@RequestPart("file") filePart: FilePart) = exampleService.save(filePart)

}

【讨论】:

  • Clement,真正对我有用的是这里的答案,我在这里问了一个问题stackoverflow.com/q/55632633/4970079
  • 只添加@RequestPart 解决了第一个错误并导致另一个不支持文件的错误。文本文件,。 pdf 什么的......你只是通过添加这个注释来解决这个问题吗?
  • @GOXR3PLUS 这不仅仅是关于注释。这也是请求部分的类是FilePart而不是MultipartFile :)
猜你喜欢
  • 2014-09-10
  • 2015-01-21
  • 2015-06-11
  • 2014-05-08
  • 2017-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-13
相关资源
最近更新 更多