Wednesday, January 8, 2014

Scala/Spray ReactiveMongo Recipe: Spray Path Matcher for BSONObjectID

This is a pretty simple one. Spray is implemented such that it is very easy to extend with nothing more than Scala code. In this case I wanted to provide an endpoint that could take a Mongo ObjectID as a path parameter. I had some other endpoint that were taking string patterns as the path parameter so I couldn't just use a String matcher without having special logic to tell which specific endpoint the user really wanted to hit. So I created a new Spray PathMatcher which will try to parse the input as an ObjectID and create a rejection if it hits a parse error. This way Spray can continue looking for another endpoint that might be a better match for this particular request.