1

Hi I have a spring webflux application. I wanted an API for uploading a file. I followed the instruction in Spring Webflux 415 with MultipartFile

And wrote something like this

@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Mono<Void> save(@RequestPart("file") Mono<FilePart> file) {
        log.info("Storing a new file. Recieved by Controller");
        this.storageService.store(file);
        return Mono.empty();
    }

But still i cannot test this, it fails with the below error in postman and swagger doesnt generate file browse button for this API.

org.springframework.web.server.UnsupportedMediaTypeStatusException: Response status 415 with reason "Content type 'image/png' not supported"
at org.springframework.web.reactive.result.method.annotation.AbstractMessageReaderArgumentResolver.readBody(AbstractMessageReaderArgumentResolver.java:206) ~[spring-webflux-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.web.reactive.result.method.annotation.AbstractMessageReaderArgumentResolver.readBody(AbstractMessageReaderArgumentResolver.java:124) ~[spring-webflux-5.0.4.RELEASE.jar:5.0.4.RELEASE]
Lucia
  • 791
  • 1
  • 8
  • 17

2 Answers2

0

could you try to set the content type to multipart/form-data in postman? when sending following request, the upper configuration works for me

    POST /rest/upload HTTP/1.1
    Host: localhost:8083
    Content: multipart/form-data
    Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

     ----WebKitFormBoundary7MA4YWxkTrZu0gW
     Content-Disposition: form-data; name="file"; filename="image.png"
     Content-Type: image/png

     (data)
     ----WebKitFormBoundary7MA4YWxkTrZu0gW

or curl:

curl --location --request POST 'http://localhost:8083/rest/upload' \
--header 'Content: multipart/form-data' \
--header 'Content-Type: multipart/form-data; boundary=--------------------------947246331766984894381774' \
--form 'file=@/Users/user/Documents/image.png'
pero_hero
  • 2,881
  • 3
  • 10
  • 24
0

I am using this which is working:

@PostMapping(value = "/{id}/draft/target", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(OK)
public Mono<TargetsValidationResponse> uploadTargets(@PathVariable("id") ObjectId id, @RequestPart(name = "file") Mono<FilePart> file) {
    return file.flatMap(parser::parseTargets).map(...);
}

Also saw your error, but then I changed to @RequestPart and FilePart and all is fine. I can test with Postman.

WesternGun
  • 11,303
  • 6
  • 88
  • 157