mongodbspring-bootgridfs

[org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'file' is not present


I am uploading .pdf file to mongo DB, I am using GridFS Template.

FileController.java

@RequestMapping(value = "/upload", method = RequestMethod.POST, 
produces = MediaType.APPLICATION_JSON_VALUE)
 public ResponseEntity<?> 
 upload(@RequestParam("file")MultipartFile file) throws 
 IOException 
  {
    return new ResponseEntity<>(fileService.addFile(file), 
    HttpStatus.OK);
}

FileService.java

public String addFile(MultipartFile upload) throws IOException {

    //define additional metadata
    DBObject metadata = new BasicDBObject();
    metadata.put("fileSize", upload.getSize());

    //store in database which returns the objectID
    Object fileID = template.store(upload.getInputStream(), upload.getOriginalFilename(), upload.getContentType(), metadata);

    //return as a string
    return fileID.toString();
}

After writing controller and service method, I am getting this error:

[org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'file' is not present]

enter image description here

Can anyone please help me, why I am getting this exception?

Thank you


Solution

  • As on the server side in the controller you've mentioned @RequestParam("file") MultipartFile file so you need to pass the key as file in the form-data while uploading the file from the client.