6

I'm trying to upload a file from my spring boot application to Amazon s3.

When I upload the file using POSTMAN, the files are successfully uploaded to s3. However, when I try to upload file from swagger it responds with a status code of 200 but does not upload the file and in my spring boot application, it throws a null pointer exception.

Is this CORS issue (I have even tried to enable CORS in spring boot config)? Or am I doing something wrong like need to add config to my swagger or something?

Here is the controller code -

@PostMapping(value = "/uploadFile" )
public JSONObject uploadFile(MultipartFile[] file) {
    return this.amazonClient.uploadFile(file);
}

Here is POSTMAN request which is working fine -

enter image description here

Here is the swagger request which does not work -

enter image description here

EDIT:

After making changes to the Controller method like this:

@RequestMapping(value = "/uploadFile",method = RequestMethod.POST, consumes = { "multipart/form-data" })
//@PostMapping(value = "/uploadFile" ) // consumes = MediaType.MULTIPART_FORM_DATA_VALUE
public JSONObject uploadFile(@RequestPart("file") MultipartFile[] file) {
    return this.amazonClient.uploadFile(file);
}

I am getting the following curl request now on Swagger:

curl -X POST "http://localhost:8080/storage/uploadFile" -H "accept: */*" -H "Content-Type: multipart/form-data" -F "file={}"

You can clearly see that the file parameter is going empty. Any thoughts?

Rohan Sharma
  • 324
  • 3
  • 12
  • The `Content-Type` in Swagger UI is wrong - `application/json` instead of `multipart/form-data`. Looks like your code is missing annotations that specify the `Content-Type` consumed by the operation. – Helen Mar 31 '20 at 13:32
  • @Helen Can you please direct me to that? I am not sure where to specify the `Content-Type` – Rohan Sharma Mar 31 '20 at 14:03
  • [Does this help?](https://stackoverflow.com/a/46520597/113116) – Helen Mar 31 '20 at 14:07
  • @Helen Thanks but it is not working. Please check question after Edit. I have changed my `Controller` method. – Rohan Sharma Mar 31 '20 at 14:14
  • 2
    I've just noticed you are uploading an array of files. This is [not supported](https://stackoverflow.com/q/33933219/113116) in Springfox 2/OpenAPI 2.0. – Helen Mar 31 '20 at 14:21
  • @Helen That makes sense. Thanks a ton for your help. – Rohan Sharma Mar 31 '20 at 14:23

1 Answers1

1

I think that , Swagger does not support properly with file uploading or downloading when I tried to upload and download file, uploading works fine but when I download it with swagger this file can not open but, using with postman everything work

hsynuls
  • 21
  • 3