Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring - Uploading file not working in swagger

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?

like image 1000
Rohan Sharma Avatar asked Mar 31 '20 13:03

Rohan Sharma


People also ask

What is spring boot MultipartFile?

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 ...

How do I connect swagger to spring boot?

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.


Video Answer


1 Answers

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

like image 52
hsynuls Avatar answered Oct 23 '22 11:10

hsynuls