Spring WebFlux-bodyType = org.springframework.web.multipart.MultipartFile不支持内容类型'application / xml'

时间:2019-04-11 12:33:04

标签: spring spring-boot spring-webflux spring-web

我正在使用spring-webflux,并希望上传文件....仅使用spring-web一切都很好,但是当涉及webflux时,我不知道出了什么问题。

小心区别...我正在使用:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>

因此,假设我们有下面的@RestController,对于Spring Web来说,它的作用就像是魅力:

@PostMapping(value = "/uploadFile")
public Response uploadFile(@RequestParam("file") MultipartFile file) {

}

现在对Spring-webflux进行尝试会产生以下错误:

{
    "timestamp": "2019-04-11T13:31:01.705+0000",
    "path": "/upload",
    "status": 400,
    "error": "Bad Request",
    "message": "Required MultipartFile parameter 'file' is not present"
}

我从一个random堆栈溢出问题中发现,我必须使用@RequestPart而不是@RequestParam,但是现在出现以下错误,我不知道为什么会发生这种情况?

错误如下:

{
    "timestamp": "2019-04-11T12:27:59.687+0000",
    "path": "/uploadFile",
    "status": 415,
    "error": "Unsupported Media Type",
    "message": "Content type 'application/xml' not supported for bodyType=org.springframework.web.multipart.MultipartFile"
}

即使有.txt个文件,也会产生相同的错误:

{
    "timestamp": "2019-04-11T12:27:59.687+0000",
    "path": "/uploadFile",
    "status": 415,
    "error": "Unsupported Media Type",
    "message": "Content type 'application/xml' not supported for bodyType=org.springframework.web.multipart.MultipartFile"
}

下面是 Postman Configuratio (邮递员配置),它很简单,我只是通过邮寄请求进行呼叫,并且仅修改了正文,如图所示。

enter image description here

通过这种方式,我也在application.properties上添加了所需的属性:)

## MULTIPART (MultipartProperties)
# Enable multipart uploads
spring.servlet.multipart.enabled=true
# Threshold after which files are written to disk.
spring.servlet.multipart.file-size-threshold=2KB
# Max file size.
spring.servlet.multipart.max-file-size=200MB
# Max Request Size
spring.servlet.multipart.max-request-size=215MB

1 个答案:

答案 0 :(得分:2)

documentation表示:

  

DefaultServerWebExchange使用配置的HttpMessageReader<MultiValueMap<String, Part>>将多部分/表单数据内容解析为MultiValueMap。

     

要以流方式解析多部分数据,可以改用HttpMessageReader返回的Flux。

只需几句话,您需要执行以下操作:

    @RequestMapping(path = "/uploadFile", method = RequestMethod.POST, 
        consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Flux<String> uploadFile(@RequestBody Flux<Part> parts) {
    //...
    }

看看这个example