Spring WebFlux-bodyType=org.springframework.web.multipart.MultipartFile不支持应用程序/xml;内容类型 [英] Spring WebFlux - Content type 'application/xml' not supported for bodyType=org.springframework.web.multipart.MultipartFile

查看:17
本文介绍了Spring WebFlux-bodyType=org.springframework.web.multipart.MultipartFile不支持应用程序/xml;内容类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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"
}

下面是邮递员配置n,非常简单,我只是用POST请求调用,并且只修改了正文,如图所示。

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

推荐答案

ASdocumentationSAI:

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

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

用很少的话你需要做这样的事情:

    @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不支持应用程序/xml;内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆