3

Use-case

I'm writing a simple REST api to evaluate Scalatra for my needs. I have two controllers (servlets?), mounted to /items/* and /categories/* in ScalatraBootstrap. I want a GET request to /items/1 to return something like this:

{
   "name": "item1",
   "category": {
     "url": "/categories/1"
   }
}

I can't find a way to generate /categories/1. The last section in http://www.scalatra.org/2.2/guides/http/reverse-routes.html looks like what I need, except it doesn't describe a way to use the functionality across servlets.

What I tried

  • Created a companion object for the servlet responsible for /categories, and tried to use the Route from there. I got an exception with the message requirement failed: routeBasePath requires the servlet to be initialized. This leads me to believe that there's some conceptual limitation that prevents this usage, but then the documentation says that ScalateUrlGeneratorSupport does even more than this. I'm confused.
  • Copy-pasted the trait removed in https://github.com/ornicar/scalatra/commit/625f8a287d466122d8a0774b71188a62ec07ecb6, mixed it into the items servlet, tried to get the route based on the route name. Resulting exception: scala.runtime.BoxedUnit cannot be cast to org.scalatra.Route

To make the question explicit: is there a way to use a named Route from one servlet to generate a URL in another servlet?

(Please feel free to correct my terminology, I'm coming pretty fresh from Python-land)

abesto
  • 2,331
  • 16
  • 28

0 Answers0