Note that there are some explanatory texts on larger screens.

plurals
  1. POScalatra: Generate reverse route from another Servlet
    primarykey
    data
    text
    <h2>Use-case</h2> <p>I'm writing a simple REST api to evaluate Scalatra for my needs. I have two controllers (servlets?), mounted to <code>/items/*</code> and <code>/categories/*</code> in <code>ScalatraBootstrap</code>. I want a <code>GET</code> request to <code>/items/1</code> to return something like this:</p> <pre><code>{ "name": "item1", "category": { "url": "/categories/1" } } </code></pre> <p>I can't find a way to generate <code>/categories/1</code>. The last section in <a href="http://www.scalatra.org/2.2/guides/http/reverse-routes.html" rel="nofollow">http://www.scalatra.org/2.2/guides/http/reverse-routes.html</a> looks like what I need, except it doesn't describe a way to use the functionality across servlets.</p> <h2>What I tried</h2> <ul> <li>Created a companion object for the servlet responsible for <code>/categories</code>, and tried to use the <code>Route</code> from there. I got an exception with the message <code>requirement failed: routeBasePath requires the servlet to be initialized</code>. This leads me to believe that there's some conceptual limitation that prevents this usage, but then the documentation says that <code>ScalateUrlGeneratorSupport</code> does even more than this. I'm confused.</li> <li>Copy-pasted the trait removed in <a href="https://github.com/ornicar/scalatra/commit/625f8a287d466122d8a0774b71188a62ec07ecb6" rel="nofollow">https://github.com/ornicar/scalatra/commit/625f8a287d466122d8a0774b71188a62ec07ecb6</a>, mixed it into the <code>items</code> servlet, tried to get the route based on the route name. Resulting exception: <code>scala.runtime.BoxedUnit cannot be cast to org.scalatra.Route</code></li> </ul> <p>To make the question explicit: is there a way to use a named <code>Route</code> from one servlet to generate a URL in another servlet?</p> <p>(Please feel free to correct my terminology, I'm coming pretty fresh from Python-land)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload