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

标签 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 中找到的我必须使用的 stackoverflow 问题 @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 配置 n 这是非常直接的,我只是通过 post 请求调用并且只修改了正文,如图所示。

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

最佳答案

documentation说:

The DefaultServerWebExchange uses the configured HttpMessageReader<MultiValueMap<String, Part>> to parse multipart/form-data content into a MultiValueMap.

To parse multipart data in streaming fashion, you can use the Flux returned from an HttpMessageReader instead.


用几句话,你需要做这样的事情:
    @RequestMapping(path = "/uploadFile", method = RequestMethod.POST, 
        consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Flux<String> uploadFile(@RequestBody Flux<Part> parts) {
    //...
    }
看这个example

关于Spring WebFlux - bodyType=org.springframework.web.multipart.MultipartFile 不支持内容类型 'application/xml',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55632633/

相关文章:

java - Tomcat java.io.IOException : Invalid keystore format when loading keystore via Classloader 异常

java - 如何创建Kafka消费者库来消费多个主题

java - WebFlux WebClient中测试状态码时如何获取响应体?

java - 使用 webclient 和 Flux 进行多个异步剩余分页调用

java - 将 Spring boot 1.5 迁移到 2.x 无法启动应用程序

spring-webflux - 因为我的基本URI不固定,所以在Webflux中一次又一次地创建Webclient是否明智?

java - 没有重定向的 Spring Security

java - 将数组列映射到 Projection 接口(interface) : No Dialect mapping for JDBC type: 2003

java - ClassNotFoundException : org. springframework.cache.Cache$ValueRetrievalException 异常

tomcat - Spring + thymeleaf 渲染HTML表单标签失败