I am new to both Spring and the REST API overall but I am now trying to make one. So from my controller, I want to return a list of files that another method returns in another class. The code probably says more then I can explain:
@RequestMapping("/backup")
public @ResponseBody List<FileInfo> backupFiles() {
return //Here i want to return the list of files
}
This is in my restController the "method" i want to return a list of FileInfo and today it already exists a method that does this that looks like this
private List<String> listBackupFiles() {
List<FileInfo> files = util.listBackupFilesInLocalDir(localStorage);
fileNameToSize = files.stream()
.collect(toMap(f -> f.name, f -> f.size));
return files.stream()
.map(f -> f.name)
.collect(toList());
}
So basically, I want to when someone goes to /backup I want the above method to trigger and return the list of files to my restController that then returns it to the requester. I don't know if this is even possible or if there is a better way to do this. I take any tips on how to tackle this problem.
listBackupFiles()method is in a separate class from your controller, you could make a new instance of that class in the controller or if you are using spring beans, you can @Autowire the spring managed instance into your controller.