Reputation:
Please help if you have any idea why its not working. I have been trying to test it with Postman for hours but no luck.
ImportController:
package demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.springframework.web.multipart.MultipartFile;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.IOException;
import javax.annotation.PostConstruct;
import javax.servlet.annotation.MultipartConfig;
@RestController
@RequestMapping("/imports")
@MultipartConfig(fileSizeThreshold = 20971520) //20MB
public class ImportController {
@Autowired
private ImportRepository importRepository;
@PostConstruct
public void init() {
System.out.println("ImportController initialized.");
}
// Create a new import
@PostMapping
@RequestMapping(value = "/", method = RequestMethod.POST, consumes = "multipart/form-data")
public ImportModel createImport(@RequestParam("file") MultipartFile file) throws IOException {
// Save the file to the server's file system
Path filePath = Paths.get("", file.getOriginalFilename());
file.transferTo(filePath);
// Create an ImportModel object with the file path and save it to the database
ImportModel importModel = new ImportModel();
importModel.setFilename(file.getOriginalFilename());
importModel.setFilePath(filePath.toString());
return importRepository.save(importModel);
}
This is the Controller that has the request and supposed to have the mappings
Upvotes: 0
Views: 1298
Reputation: 455
There could be a possible situation where in spring boot you forgot to add @RestController to the API implementation class.
Upvotes: 0
Reputation: 1151
Your code is expecting "/imports/".
On the class:
@RequestMapping("/imports")
and then again on the method:
@PostMapping
@RequestMapping(value = "/", . . . .
Forget the 2nd @RequestMapping
and just use
@PostMapping(consumes = "multipart/form-data")
There is really no need to mix it all up and do double definitions.
Upvotes: 0