1

I'am relatively new to java and want to try out something new. At the moment I'am trying out the java framework play.

What I'am trying to do is to rewrite a little REST JSON Api written in php as a play rest service.

I'am using mongodb as datastore and therefore I installed morphia. Everything is working as expected. But I'am a little bit confused about the json result renderJSON() produces.

Here is the code of my model:

@AutoTimestamp
@Entity
public class Bookmark extends Model {
public String title;
public String url;
public String description;
public List tags;
public boolean is_private;

public Bookmark(String title,
                String url,
                String description,
                String tags,
                boolean is_private)
{
    this.title          = title;
    this.url            = url;
    this.description    = description;
    this.tags           = Arrays.asList(tags.split(" "));
    this.is_private     = is_private;
}
 }

To retrieve all bookmarks I'am using the following method within the controller:

    public static void listAll() {
        List<Bookmark> bookmarks = Bookmark.findAll();
        renderJSON(bookmarks);
    }

Here is a line of the result:

[{"title":"Test","url":"http://www.google.de","description":"test","tags":["tag1","tag2","tag3","tag5","tag0209135913598"],"is_private":true,"_id":{"_time":1331202670,"_machine":-637116226,"_inc":989037616,"_new":false},"_created":1331202670469,"_modified":1331202670469,"blobFieldsTracker":{}}]

I'am a little bit confused now about the "_id"-Object with the attributes "_time", "_machine" and so on. And I'am also confused about "blobFieldsTracker".

For the attribute "_id" I would expect a mongoid. I could not find anything related to that within the docs or with google so my question is what I have to do to include the "real" mongoid within the json-string.

Marc
  • 151
  • 1
  • 7
  • 16

1 Answers1

1

Please use the following render statement:

renderJSON(bookmarks, new play.modules.morphia.utils.ObjectIdGsonAdapter());

About the blobFieldsTracker, it's a housekeeping field morphia plugin enhanced to your model class. For now, just ignore it in your javascript.

Gelin Luo
  • 14,035
  • 27
  • 86
  • 139
  • In the latest version you can use String type ID: http://www.playframework.org/modules/morphia-1.2.8-RC1/model#id – Gelin Luo Jun 15 '12 at 08:23