I am currently making a file upload within my spring boot project. I am using rest controller for my controller as that is what is written within the tutorial that I am using https://www.callicoder.com/spring-boot-file-upload-download-jpa-hibernate-mysql-database-example/
However, I found out that apparently rest controller unable to display HTML page based on what is written here: How to return a html page from a restful controller in spring boot?
Is there a way for displaying my HTML page while retaining the rest controller?
this is my controller
@RestController
public class RekonsiliasiController {
private static final Logger logger = LoggerFactory.getLogger(RekonsiliasiController.class);
@Autowired
private DBFileStorageService dbFileStorageService;
@RequestMapping("/rekonsiliasi")
public String index() {
System.err.println("MASUK PAK EKO OI");
return "rekonsiliasi";
}
@PostMapping("/uploadFile")
public UploadFileResponse uploadFile(@RequestParam("file") MultipartFile file) {
DBFile dbFile = dbFileStorageService.storeFile(file);
String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath()
.path("/downloadFile/")
.path(dbFile.getId())
.toUriString();
return new UploadFileResponse(dbFile.getFileName(), fileDownloadUri,
file.getContentType(), file.getSize());
}
@GetMapping("/downloadFile/{fileId}")
public ResponseEntity<ByteArrayResource> downloadFile(@PathVariable String fileId) {
// Load file from database
DBFile dbFile = dbFileStorageService.getFile(fileId);
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(dbFile.getFileType()))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + dbFile.getFileName() + "\"")
.body(new ByteArrayResource(dbFile.getData()));
}
}
and this is my rekonsiliasi.html
<form method="POST" action="/uploadFile" enctype="multipart/form-data">
<input type="file" name="file" /><br/><br/>
<input type="submit" value="Submit" />
</form>
This is what I get currently, a blank page with a simple text
UPDATE
I tried to divide between the index and the file upload to the following.
RekonsiliasiController.java
@Controller
public class RekonsiliasiController {
@RequestMapping("/rekonsiliasi")
public String index() {
System.err.println("MASUK PAK EKO OI");
return "rekonsiliasi";
}
FileController.java
@RestController
public class FileController {
private static final Logger logger = LoggerFactory.getLogger(FileController.class);
@Autowired
private DBFileStorageService dbFileStorageService;
@PostMapping("/uploadFile")
public UploadFileResponse uploadFile(@RequestParam("file") MultipartFile file) {
System.err.println("CEK");
DBFile dbFile = dbFileStorageService.storeFile(file);
String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath()
.path("/downloadFile/")
.path(dbFile.getId())
.toUriString();
return new UploadFileResponse(dbFile.getFileName(), fileDownloadUri,
file.getContentType(), file.getSize());
}
@GetMapping("/downloadFile/{fileId}")
public ResponseEntity<Resource> downloadFile(@PathVariable String fileId) {
// Load file from database
DBFile dbFile = dbFileStorageService.getFile(fileId);
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(dbFile.getFileType()))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + dbFile.getFileName() + "\"")
.body(new ByteArrayResource(dbFile.getData()));
}
}
Now the HTML is showing perfectly fine but when I upload the file I got 403 Error. Before I tried to find the problem for this part, I'd like to know whether if there are some ways for displaying my HTML page while retaining the rest controller?
EDIT Delete the 'uploadMultipleFiles' method in FileController.Java and it still gets me 403 error