Trying to create a Restful API

156 views
Skip to first unread message

John Polling

unread,
May 17, 2013, 1:20:53 PM5/17/13
to scalat...@googlegroups.com
I'm very new to Scala and Scalatra.  I'm slowly getting my head around it all.

I'm creating a restful API with a MongoDB.  I'm using JackonJSON to help with JSON parsing etc, however I'm finding that any ObjectId's aren't being output by the API.  I just a blank value, however if I println the values they exist within the object, so I'm certain it's a deserialization issue?

Thanks

John

Stefan Ollinger

unread,
May 20, 2013, 6:16:57 PM5/20/13
to scalat...@googlegroups.com
Hi,
can you paste the classes you want to (de-)serialize along with your code?
> --
> You received this message because you are subscribed to the Google
> Groups "scalatra-user" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to scalatra-use...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

John Polling

unread,
May 21, 2013, 9:58:17 AM5/21/13
to scalat...@googlegroups.com
Sure thing.


Thanks

John

Stefan Ollinger

unread,
May 22, 2013, 7:18:02 PM5/22/13
to scalat...@googlegroups.com
Hi,

there are different options you have:

  - create a JValue manually with either Json4s' Extraction.decompose or salat's grater
  - return the Milestone and let Scalatra's render pipeline handle the conversion (calls Extraction.decompose internally)

In the case that you use Json4s, you need to add a custom serializer, so that Json4s knows how to handle ObjectId instances.

Here is a sample: https://gist.github.com/dozed/5631680

Regards,
Stefan

Stefan Ollinger

unread,
May 22, 2013, 7:29:12 PM5/22/13
to scalat...@googlegroups.com
Also Json4s already has a mongo module, which you can use. It is called: json4s-mongo
You need to add it to your project dependencies. I updated the gist.

John Polling

unread,
May 23, 2013, 9:30:47 AM5/23/13
to scalat...@googlegroups.com
Thanks so much for this Stefan.

Learning lots and fast :)
Reply all
Reply to author
Forward
0 new messages