繁体   English   中英

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

[英]Spring WebFlux - Content type 'application/xml' not supported for bodyType=org.springframework.web.multipart.MultipartFile

我正在使用spring-webflux并希望上传文件..一切spring-webfluxspring-web运行良好,但是当涉及到webfluxwebflux了什么问题。

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

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

假设我们有下面的@RestController ,对于Spring Web来说,它的工作原理类似于charm:

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

}

现在对Spring-webflux尝试同样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"
}

我从一个随机的 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"
}

下面是邮递员配置 ,这很简单,我只是用邮寄请求进行呼叫,并且只修改了正文,如图所示。

在此处输入图片说明

顺便说一句,我也在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

正如文档所说:

DefaultServerWebExchange使用配置的HttpMessageReader<MultiValueMap<String, Part>>将multipart / form-data内容解析为MultiValueMap。

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

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

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

看这个例子

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM