Home » Java » java – Spring webflux – file upload doesnt work it throws error when i hit it from postman-Exceptionshub

java – Spring webflux – file upload doesnt work it throws error when i hit it from postman-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

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]
How to&Answers:

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 '[email protected]/Users/user/Documents/image.png'