从 Scalatra 切换到 Spray:处理 notFound 和 spray 中的错误?
Switching from Scalatra to Spray : Handling notFound and errors in spray?
我们是 Scalatra 的用户。每次我们创建一个 servlet 时,我们都会扩展我们的 BaseServlet 扩展 ScalatraBase :
trait BaseServlet extends ScalatraFilter with ScalateSupport with FlashMapSupport {
/**
* Returns the request parameter value for the given argument.
*/
def getParam(key:String)(implicit request: HttpServletRequest): Option[String] = Option(request.getParameter(key))
notFound {
// If no route matches, then try to render a Scaml template
val templateBase = requestPath match {
case s if s.endsWith("/") => s + "index"
case s => s
}
val templatePath = "/WEB-INF/templates/" + templateBase + ".scaml"
servletContext.getResource(templatePath) match {
case url: URL =>
contentType = "text/html"
templateEngine.layout(templatePath)
case _ =>
filterChain.doFilter(request, response)
}
}
error {
case e:ControlThrowable => throw e
case e:Throwable =>
val errorUID:String = UUID.randomUUID.getLeastSignificantBits.abs.toString
Log.logger(Log.FILE.ALL_EXCEPTIONS).error("#"+ errorUID + " -- " + e.getMessage + e.getStackTraceString)
contentType = "application/json"
response.setStatus(500)
JsonUtility.toJSONString( Map("message" -> ("Server Error # "+ errorUID ) , "reason" -> e.getMessage ))
}
}
编辑:
我想把它抽象出来。我的意思是我想在我的 BaseServlet 中添加所有错误和拒绝处理功能,然后扩展它(比如 AnyServlet)。因此,如果 AnyServlet 有未找到的路径或在某处抛出异常,它会由 BaseServlet 自动处理。
Spray 中是否有类似的东西可以以类似的方式处理我找不到的路径和错误?
提前致谢!
您必须定义自定义 RejectionHandler。
在您的应用程序中将此 RejectionHandler 定义为隐式值。
import spray.routing.RejectionHandler
private implicit val notFoundRejectionHandler = RejectionHandler {
case Nil => {
// If no route matches, then try to render a Scaml template...
}
}
喷出来的githubspec
你不需要 "abstract it out" 因为在喷雾中你没有明显的 "servlets" - 你只有一条路线,它可能会调用其他路线:
class UserRoute {
val route: Route = ...
}
class DepartmentRoute {
val route: Route = ...
}
class TopLevelRoute(userRoute: UserRoute, departmentRoute: DepartmentRoute) {
val route: Route =
(handleRejections(MyRejHandler) & handleExceptions(MyExHandler)) {
path("users") {
userRoute.route
} ~
path("departments") {
departmentRoute.route
}
}
}
您可以将处理程序放在 TopLevelRoute 中,它将应用于 UserRoute 或 DepartmentRoute 中的任何内容。 spray HttpServiceActor
只处理一条路线,而不是一堆不同的路线 "controllers" - 如何将所有路线组合成一条路线取决于您。
我们是 Scalatra 的用户。每次我们创建一个 servlet 时,我们都会扩展我们的 BaseServlet 扩展 ScalatraBase :
trait BaseServlet extends ScalatraFilter with ScalateSupport with FlashMapSupport {
/**
* Returns the request parameter value for the given argument.
*/
def getParam(key:String)(implicit request: HttpServletRequest): Option[String] = Option(request.getParameter(key))
notFound {
// If no route matches, then try to render a Scaml template
val templateBase = requestPath match {
case s if s.endsWith("/") => s + "index"
case s => s
}
val templatePath = "/WEB-INF/templates/" + templateBase + ".scaml"
servletContext.getResource(templatePath) match {
case url: URL =>
contentType = "text/html"
templateEngine.layout(templatePath)
case _ =>
filterChain.doFilter(request, response)
}
}
error {
case e:ControlThrowable => throw e
case e:Throwable =>
val errorUID:String = UUID.randomUUID.getLeastSignificantBits.abs.toString
Log.logger(Log.FILE.ALL_EXCEPTIONS).error("#"+ errorUID + " -- " + e.getMessage + e.getStackTraceString)
contentType = "application/json"
response.setStatus(500)
JsonUtility.toJSONString( Map("message" -> ("Server Error # "+ errorUID ) , "reason" -> e.getMessage ))
}
}
编辑: 我想把它抽象出来。我的意思是我想在我的 BaseServlet 中添加所有错误和拒绝处理功能,然后扩展它(比如 AnyServlet)。因此,如果 AnyServlet 有未找到的路径或在某处抛出异常,它会由 BaseServlet 自动处理。 Spray 中是否有类似的东西可以以类似的方式处理我找不到的路径和错误? 提前致谢!
您必须定义自定义 RejectionHandler。
在您的应用程序中将此 RejectionHandler 定义为隐式值。
import spray.routing.RejectionHandler
private implicit val notFoundRejectionHandler = RejectionHandler {
case Nil => {
// If no route matches, then try to render a Scaml template...
}
}
喷出来的githubspec
你不需要 "abstract it out" 因为在喷雾中你没有明显的 "servlets" - 你只有一条路线,它可能会调用其他路线:
class UserRoute {
val route: Route = ...
}
class DepartmentRoute {
val route: Route = ...
}
class TopLevelRoute(userRoute: UserRoute, departmentRoute: DepartmentRoute) {
val route: Route =
(handleRejections(MyRejHandler) & handleExceptions(MyExHandler)) {
path("users") {
userRoute.route
} ~
path("departments") {
departmentRoute.route
}
}
}
您可以将处理程序放在 TopLevelRoute 中,它将应用于 UserRoute 或 DepartmentRoute 中的任何内容。 spray HttpServiceActor
只处理一条路线,而不是一堆不同的路线 "controllers" - 如何将所有路线组合成一条路线取决于您。