Skip to content

Instantly share code, notes, and snippets.

@stephanie-gredell
Last active August 29, 2015 14:17
Show Gist options
  • Save stephanie-gredell/88fa2dd8a777cf82f4d9 to your computer and use it in GitHub Desktop.
Save stephanie-gredell/88fa2dd8a777cf82f4d9 to your computer and use it in GitHub Desktop.
Return JSON with EBean and Play
//return a collection of items
public static Result getAssets() {
List<Asset> asset = Ebean.find(Asset.class)
.findList();
return ok(toJson(asset));
}
//or return a single item with the id being passed as a url param
public static Result getAsset(Integer id) {
Asset asset = Ebean.find(Asset.class, id);
if (asset == null) {
return internalServerError("Not Found");
}
return ok(toJson(asset));
}
//or you could even filter by a field in the database
public static Result getRoadmaps() {
List<Roadmap> roadmaps = Ebean.find(Roadmap.class)
.where()
.eq("type", "roadmap")
.findList();
return ok(toJson(roadmaps));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment