0

I'm creating an api for sending email in Spring Boot. I can successfully send an attachment in email using the following api

@PostMapping("/send")
public void sendMail(@RequestParam(value = "receiver") String receiver,
        @RequestParam(value = "subject") String subject, @RequestParam(value = "content") String content,
        @RequestParam(value = "file", required = false) MultipartFile file) {
    mailService.send(receiver, subject, content, file);
}

But an email can have multiple attachments. So, using this link as the reference, I updated my code to

@PostMapping("/send")
public void sendMail(@RequestParam(value = "receiver") String receiver,
        @RequestParam(value = "subject") String subject, @RequestParam(value = "content") String content,
        @RequestParam(value = "files", required = false) MultipartFile[] files) {
    mailService.send(receiver, subject, content, files);
}

With this in place, I can add multiple images from the Swagger UI

Update: I get the following form in Swagger from which I can upload images

Swagger UI form

But when I submit the form, I found that the value in files is now null instead of an array of files.

What am I missing?

Kshitij Bajracharya
  • 811
  • 2
  • 14
  • 37
  • Did you try using List instead of array @RequestBody List files and uploaded files as RequestBody instead of RequestParam – Mebin Joe Mar 19 '19 at 09:17
  • @MebinJoe yes. Same result. – Kshitij Bajracharya Mar 19 '19 at 09:18
  • are you always getting null? – Mebin Joe Mar 19 '19 at 09:19
  • are you using HTTP POST. Also add enctype="multipart/form-data" – Mebin Joe Mar 19 '19 at 09:22
  • Tried this: `@RequestMapping(value = "/attachment", method = RequestMethod.POST, consumes = "multipart/form-data") public void upload(@RequestParam List files) { for (MultipartFile file : files) { System.out.println(file.getOriginalFilename()); } }` and now I get CollectionsEmptyList in files – Kshitij Bajracharya Mar 19 '19 at 09:30
  • Try @RequestParam("files") List files instead of MultipartFile[] files – Shashikant Sharma Mar 19 '19 at 09:30
  • how do you upload files or how you hit the API. Using any API client or from browser ? – Mebin Joe Mar 19 '19 at 09:32
  • @MebinJoe I'm using the swagger ui. I've added a screenshot in the op. – Kshitij Bajracharya Mar 19 '19 at 09:45
  • @ShashikantSharma tried that as well but no luck. – Kshitij Bajracharya Mar 19 '19 at 09:45
  • You did a mistake in your second code snippet replace Request param name `files` with `files' . Example `@RequestParam(value = "file", required = false)` instead of `@RequestParam(value = "files", required = false)`. – Sudhir Ojha Mar 19 '19 at 09:56
  • 1
    I think this is a known issue with swagger. https://github.com/swagger-api/swagger-editor/issues/467, https://github.com/RSuter/NSwag/issues/845. Try latest version as a workaround – Mebin Joe Mar 19 '19 at 09:58
  • @KshitijBajracharya In your swagger UI for content value did you try 'multipart/form-data' ? Swagger supports file uploads sent with Content-Type: multipart/form-data. That is, your API server must consume multipart/form-data for this operation. Ideally this should solve your problem. – Mebin Joe Mar 19 '19 at 10:00
  • Yes I've added multipart/form-data with `@RequestMapping(value = "/send", method = RequestMethod.POST, consumes = "multipart/form-data")` Even with that, the payload of my request when adding single MultipartFile is `content: asdasd file: (binary) receiver: asd@adasd.com subject: aaa` whereas the payload when I'm passing an array of files is `content: asdasd files: {},{} receiver: asd@adasd.com subject: aaa` – Kshitij Bajracharya Mar 19 '19 at 10:28

1 Answers1

0

As @MebinJoe mentioned, it was an issue with swagger. Couldn't solve the issue with swagger but ended up using Postman for testing the above piece of code. Multiple files were successfully attached and sent in email.

Kshitij Bajracharya
  • 811
  • 2
  • 14
  • 37
  • Please raise an issue in swagger Github and followup. Might get resolved in later versions as single file upload is working fine. Could you please mark my answer as useful. Thank you – Mebin Joe Mar 21 '19 at 05:13
  • One more thing. In the swagger UI, did you try setting content string value as 'multipart/form-data' ? – Mebin Joe Mar 21 '19 at 05:15
  • @MebinJoe, yes I tried 'multipart/form-data' but it still didn't work in swagger. – Kshitij Bajracharya Mar 21 '19 at 05:23