Search code examples
javabackbone.jsplayframework-2.0backbone.js-collections

Play Java Backbone.js collection


I am using the Play 2 framework to try and set up a simple todo app with Backbone.JS. I found the code on Play's site for handling a single request from backbone.

@BodyParser.Of(BodyParser.Json.class)
public static Result getList() {

    ObjectNode result = Json.newObject();

    String name = " Matt";
    if(name == null) {
        result.put("status", "KO");
        result.put("message", "Missing parameter [name]");
        return badRequest(result);
    } else {
        result.put("status", "Ok");
        result.put("message", "Hello " + name);
        return ok(result);
    }
}

However, I cannot figure out how to return a whole collection. I have tried putting the ObjectNode in an array and returning that. I'm just beginning with restful api's and could use some help. I have searched all over for a solution but have come up empty. I would greatly appreciate some help. Thanks.


Solution

  • You can change your object(including collection) in the JSON, Play has inbuilt Jackson integration

    try this

    return ok(Json.toJson(obj)); // prepare you object obj and return it like this