Search code examples
routesplayframework-2.2

How do I route an A-record to a specific controller action in Playframework 2.2.x?


Basically, I wish to route all requests to a specific A-record (root domain record, e.g. http://example.com) to a specific controller action (controllers.Application.other()) in my Playframework 2.2.1 application.

Following the explanation over here, I understand I need to override onRouteRequest in my Global object and do the injected route over there.

But for the life of me, I can't get James Roper's example to work (I assume it's because of the Playframework version). The overriden function looks as follows:

import ....

object Global extends WithFilters(requireSSL, new GzipFilter()) with GlobalSettings {

  override def onRouteRequest(request: RequestHeader): Option[Handler] = {
    (request.method, request.path) match {
      case ("GET", "example.com") => Some(controllers.Application.other)
      case _ => super.onRouteRequest(request)
    }
  }
}

    ....

which yields:

type mismatch; found : play.mvc.Result required: play.api.mvc.Handler

My controllers.Application.other action is extremely basic, only rendering a page:

public static Result other() {
  return ok(views.html.indexOther());
}

I also tried changing controllers.Application.other to controllers.routes.Application.other, but then it says it needs a play.api.mvc.Handler instead of a ...Call, which is to be expected I guess.

I found this post on changing aspects of the request headers in Playframework 2.2.1 and I think it could help me solve my problem, but I can't wrap my head around representing the controller action I want to redirect the request to as a play.api.mvc.Handler :(.

Any help or explanation is greatly appreciated!


Solution

  • To answer my own question: we haven't been able to find a solution. We eventually decided to use a Scala controller to point the custom routes in the Global onRouteRequest override to. Works like a charm. Although mixing Scala and Java controllers feels a bit itchy.