我們是Scalatra用戶。它 我想抽象出來:每次我們將創建一個servlet,我們將擴大我們的BaseServlet延伸ScalatraBase:從Scalatra切換到Spray:處理not spray和錯誤噴霧?
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中有沒有類似的方式可以處理我找不到的路徑和錯誤? 在此先感謝!
請參閱噴塗文檔中的「拒收處理」和「異常處理」。 http://spray.io/documentation/1.2.2/spray-routing/key-concepts/rejections/ – jrudolph 2015-02-24 08:51:32