Compojure and mongodb for API


#1

Hello

I am currently looking into creating a JSON api with compojure and was wondering if anyone has got good examples laying around.

I currently use compojure (handler/api) and monger.

But serializing ObjectId is not working.

I saw that there is a compojure-api ( https://github.com/metosin/compojure-api ) but don’t know if that is a good tool for this?


#2

Hi there!

What library are you using to write out JSON? The ones I know of all have ways of adding new serializers for different types.

Check out this: http://www.lispcast.com/json-serialization-api-clojure

If that doesn’t help, let me know what library you are using.

Thanks
Eric


#3

I just created a repo on github

So basically I am using

(middleware/wrap-json-body)
(middleware/wrap-json-response)

I started out with http://blog.interlinked.org/programming/clojure_rest.html

and am trying to switch out the in memory db with mongodb.

Have not had the time to read the article you refered to, but will do as soon as I can find the time.


#4

Ok, Here’s what I would try:

(cheshire.generate/add-encoder org.bson.types.ObjectId cheshire.generate/encode-str)

If all the namespaces are required, that should work. You will have to run it before the first web request.


#5

Thanks I got it working now, I did need to add genereate-string as well, but you can see that in the repo on github


#6

Great! I’m glad you got it working.