Returning a 404 JSON Response in Play 2.4.x

(Building a JSON API using Scala and Play)

I was having a lot of trouble setting up a "catch all" 404 Not Found Handler in Play 2.4.x. If the user was to hit a route that did not exist, it should return with a JSON response, not an ugly HTML Page.

This is finally how I got it working (Thanks Ryan):

import play.api._  
import play.api.libs.json._  
import play.api.mvc._  
import play.api.mvc.Results._  
import scala.concurrent.Future

object Global extends GlobalSettings {

  override def onHandlerNotFound(request: RequestHeader) = {
    Future.successful(NotFound(Json.obj("error" -> "Not Found")))