scala – 可以将Spray.io路线分割成多个“控制器”吗?
发布时间:2020-12-16 09:38:22 所属栏目:安全 来源:网络整理
导读:我没有找到一个坚实的例子或结构将Spray.io路由分割成多个文件。我发现目前的路由结构将变得非常繁琐,将它们抽象成一个非常简单的REST API应用程序的不同“控制器”是很好的。 文档似乎不太有帮助:http://spray.io/documentation/spray-routing/key-concep
我没有找到一个坚实的例子或结构将Spray.io路由分割成多个文件。我发现目前的路由结构将变得非常繁琐,将它们抽象成一个非常简单的REST API应用程序的不同“控制器”是很好的。
文档似乎不太有帮助:http://spray.io/documentation/spray-routing/key-concepts/directives/#directives 这是我到目前为止 class AccountServiceActor extends Actor with AccountService { def actorRefFactory = context def receive = handleTimeouts orElse runRoute(demoRoute) def handleTimeouts: Receive = { case Timeout(x: HttpRequest) => sender ! HttpResponse(StatusCodes.InternalServerError,"Request timed out.") } } // this trait defines our service behavior independently from the service actor trait AccountService extends HttpService { val demoRoute = { get { path("") { respondWithMediaType(`text/html`) { // XML is marshalled to `text/xml` by default,so we simply override here complete(index) } } ~ path("ping") { complete("PONG!") } ~ path("timeout") { ctx => // we simply let the request drop to provoke a timeout } ~ path("crash") { ctx => throw new RuntimeException("crash boom bang") } ~ path("fail") { failWith(new RuntimeException("aaaahhh")) } ~ path("riaktestsetup") { Test.setupTestData complete("SETUP!") } ~ path("riaktestfetch" / Rest) { id => complete(Test.read(id)) } } } } 感谢您的帮助! 解决方法
您可以使用?组合器组合来自不同“控制器”的路由。
class AccountServiceActor extends Actor with HttpService { def actorRefFactory = context def receive = handleTimeouts orElse runRoute( new AccountService1.accountService1 ~ new AccountService2.accountService2) def handleTimeouts: Receive = { case Timeout(x: HttpRequest) => sender ! HttpResponse(StatusCodes.InternalServerError,"Request timed out.") } } class AccountService1 extends HttpService { val accountService1 = { get { path("") { respondWithMediaType(`text/html`) { // XML is marshalled to `text/xml` by default,so we simply override here complete(index) } } } } class AccountService2 extends HttpService { val accountService2 = { get { path("someotherpath") { respondWithMediaType(`text/html`) { // XML is marshalled to `text/xml` by default,so we simply override here complete(index) } } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |