2

I have REST resources like author and article. There can be multiple Authors and each author can own multiple articles. I know it is possible to model their Scalatra handlers in one servlet like

  • /author/:id/article
  • /author/:id/article/:id

etc. but doing this my servlet file gets big. It looks that everything under author needs to be handled within one servlet.

I'd like to split this stuff into several independent servlets that would make up whole resources adresses when registered together. So I'd like to have author-related things in one servlet and article-related things in another.

Is there a way to do that in Scalatra? I found similar question regarding Sinatra but with no good answer Sub routing in Sinatra

Community
  • 1
  • 1
Michal Ostruszka
  • 2,089
  • 2
  • 20
  • 23

1 Answers1

3

You should be able to set your servlets and routes up however you like.

For example, you might set up two servlets, like this:

class AuthorsServlet extends WebStack {
  get("/authors") { }

  get("/authors/:id") { }
}

class ArticlesServlet extends WebStack {
  get("/authors/:authorId/articles") { }

  get("/authors/:authorId/articles/:id) { }

}

And then register your servlets in ScalatraBootstrap:

override def init(context: ServletContext) {
  context.mount(new AuthorsServlet, "/*")
  context.mount(new ArticlesServlet, "/*")
}
futurechimp
  • 554
  • 4
  • 6
  • 4
    Is this still valid for 2.3? Somehow it doesn't seem to work, routes are always matched on the first mounted servlet. – simao Jun 24 '15 at 11:29