问题
I am using spring-webflux
and want to upload files .... everything works great with just spring-web
but when it comes to webflux
i have not a clue what's wrong .
Be careful in the difference ... i am using :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
So let's say we have the below @RestController
, for Spring Web
it works like charm:
@PostMapping(value = "/uploadFile")
public Response uploadFile(@RequestParam("file") MultipartFile file) {
}
Now trying the same with Spring-webflux
produces the below error :
{
"timestamp": "2019-04-11T13:31:01.705+0000",
"path": "/upload",
"status": 400,
"error": "Bad Request",
"message": "Required MultipartFile parameter 'file' is not present"
}
I found from a random stackoverflow question that i have to use @RequestPart
instead of @RequestParam
but now i am getting the below error and i don't have a clue why this happens ?
The error is the below :
{
"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"
}
Even with .txt
files is producing the same error :
{
"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"
}
Below is the Postman Configuration which is pretty straight forward , i am just calling with a post request and modified only the body as shown in the picture .
By the way i have added the needed properties on application.properties too :)
## 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:
As documentation sais :
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.
With few words you need to do something like this:
@RequestMapping(path = "/uploadFile", method = RequestMethod.POST,
consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public Flux<String> uploadFile(@RequestBody Flux<Part> parts) {
//...
}
Look at this example
来源:https://stackoverflow.com/questions/55632633/spring-webflux-content-type-application-xml-not-supported-for-bodytype-org-s