0

enter image description hereI want to receive multi rows in spring boot controller, tried different approached but unable to do it. i am testing with postman.

Controller

@PostMapping(URLConstant.URL_SC_ATTACHMENT_POST)
public ResponseEntity<ApiResponse> storeFile(@RequestParam("attachmentDto") List<BudgetSceAttachmentDto> attachmentDto) throws IOException {
    System.out.println(attachmentDto);
    return ResponseUtil.getResponse(HttpStatus.OK, MsgConstant.BUDGET_MSG_FILE_UPLOADED, null);

}

DTO

private Integer versionId;

private String fileName;

private String pathUploadedFile;

private String uploadedFileName;

private MultipartFile file;

1 Answers1

0

First of all, request's content-type should be multipart/form-data.

Then Let's simplify this problem - check upload multiple file first.

@PostMapping(URLConstant.URL_SC_ATTACHMENT_POST)
public ResponseEntity<ApiResponse> storeFile(@RequestParam MultipartFile[] files) throws IOException {
    Assert.isTrue(files.length == 2, "files length should be 2");
    System.out.println(files.length);
    return ResponseUtil.getResponse(HttpStatus.OK, MsgConstant.BUDGET_MSG_FILE_UPLOADED, null);
}

If it works well, now time to bring the DTO again.

@PostMapping(URLConstant.URL_SC_ATTACHMENT_POST)
public ResponseEntity<ApiResponse> storeFile(@ModelAttribute List<BudgetSceAttachmentDto> params) throws IOException {
    Assert.isTrue(params.length == 2, "files length should be 2");
    System.out.println(params.length);
    return ResponseUtil.getResponse(HttpStatus.OK, MsgConstant.BUDGET_MSG_FILE_UPLOADED, null);
}
dgregory
  • 1,277
  • 1
  • 10
  • 25
  • thanks for your input, First approach is working fine, how to send the dto's data from postman, i am trying the same way which i have followed above, Body-> form-data and key value, but however i have DTO at controller so i am not sure how the data should be pass from postman. – KuldeeP ChoudharY Sep 11 '19 at 12:40
  • Now I understand your problem is postman. Unfortunatly, I have no idea about postman but why don't you use cURL? It looks old-fashon but simple and reliable. [cURL to upload File](https://stackoverflow.com/a/28045514/1378965) I recommand use cURL to confirm the java code first and try with postman again. – dgregory Sep 11 '19 at 12:52