[英]Spring not accepting list of multipart files: java.lang.NoSuchMethodException: org.springframework.web.multipart.MultipartFile
[英]Spring WebFlux - Content type 'application/xml' not supported for bodyType=org.springframework.web.multipart.MultipartFile
我正在使用spring-webflux
并希望上传文件..一切spring-webflux
在spring-web
运行良好,但是当涉及到webflux
我webflux
了什么问题。
小心区别...我正在使用:
<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.