1

I'm currently playing around with RestEasy and Angular.js. I'm trying to return a collection or array of objects with all the attributes in the json. But when I perform the following method below, it only returns a JSON result of file names in string format. But I want all the attributes of the File class returned. Is there an easy way to do such in JSON with RestEasy?

@GET
@Produces("application/json")
@Path("/details/{location}")
public File[] getFolderDetails(@PathParam("location") String location) {
    //List<File> folders = new ArrayList<File>();
    File folder = new File(DIRECTORY_LOCATION + "/" + location);
    File[] listOfFiles = folder.listFiles();
    File folders[] = new File[listOfFiles.length];
    int index = 0;
    for (File listOfFile : listOfFiles) {
        folders[index] = listOfFile;
        index++;
    }
    return folders;
}

Currently this method returns something like:

["C:\jboss-4.2.2.GA\server\all","C:\jboss-4.2.2.GA\server\default","C:\jboss-4.2.2.GA\server\minimal","C:\jboss-4.2.2.GA\server\plu"]

Bohemian
  • 412,405
  • 93
  • 575
  • 722
HelloWorld
  • 4,251
  • 8
  • 36
  • 60

1 Answers1

1

RESTEasy can handle basic java objects, so I would try a Map:

@GET
@Produces("application/json")
@Path("/details/{location}")
public List<Map<String, Object>> getFolderDetails(@PathParam("location") String location) {
    File folder = new File(DIRECTORY_LOCATION + "/" + location);
    File[] listOfFiles = folder.listFiles();
    List<Map<String, Object>> folders = new ArrayList<Map<String, Object>>();
    for (File file : listOfFiles) {
        Map<String, Object> item = new LinkedHashMap<String, Object>();
        item.put("name", file.getName());
        item.put("size", file.length());
        item.put("lastModified", file.lastModified());
        // etc, adding things that have values from java.lang.* package
        folders.add(folder);
    }
    return folders;
}
Bohemian
  • 412,405
  • 93
  • 575
  • 722