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 -
Here is the swagger request which does not work -
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?
public interface MultipartFile extends InputStreamSource. A representation of an uploaded file received in a multipart request. The file contents are either stored in memory or temporarily on disk. In either case, the user is responsible for copying file contents to a session-level or persistent store as and if desired ...
To enable the Swagger2 in Spring Boot application, you need to add the following dependencies in our build configurations file. For Gradle users, add the following dependencies in your build. gradle file. Now, add the @EnableSwagger2 annotation in your main Spring Boot application.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With