0

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.

3
  • 2
    I see that, the service listBackupFiles() returns a String, but in your rest controller you are returning List<FileInfo>. This is not right! Commented Mar 14, 2018 at 12:31
  • You need to create an instance of the other class and then call the method. If the 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. Commented Mar 14, 2018 at 12:31
  • you need to put the list in json before return, and use @Autowire to access the class Commented Mar 14, 2018 at 12:58

1 Answer 1

1

The controller would be:

  // @RestController = @Controller + @ResponseBody
  @RestController
    public class BackupController {  

        @Autowired
        private BackupService backupService;

        // you don't need @ResponseBody as you use @RestController
        @RequestMapping("/backup")
        public List<FileInfo> backupFiles() {
            return backupService.listBackupFiles()
        }
    }

The service interface would be:

public interface BackupService {

     public List<FileInfo> listBackupFiles();
}

The service implementation would be:

@Service
public class BackupServiceImpl implements BackupService {

    public List<FileInfo> listBackupFiles() {
        // localStorage come from
        // maybe Util has it as static method, else inject it
        return util.listBackupFilesInLocalDir(localStorage);
    }
}

Hope was helpful :)

Sign up to request clarification or add additional context in comments.

1 Comment

i get duplicate class error on the "BackupService" class and renaming fixes nothing. feels like this would work but cant get past this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.