相关疑难解决方法(0)

可以将Spray.io路由分成多个"控制器"吗?

我还没有找到一个可靠的示例或结构来将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 …
Run Code Online (Sandbox Code Playgroud)

scala spray

18
推荐指数
2
解决办法
8215
查看次数

如何在Spray中的嵌套路由中使用字符串指令提取器

在这里回答我自己的问题,因为这花了我一天多的时间来弄清楚,这是一个非常简单的问题,我认为其他人可能会遇到.

在使用REST创建RESTful-esk服务时,我希望将具有字母数字id的路由作为路径的一部分进行匹配.这是我最初开始的:

case class APIPagination(val page: Option[Int], val perPage: Option[Int])
get {
  pathPrefix("v0" / "things") {
    pathEndOrSingleSlash {
      parameters('page ? 0, 'perPage ? 10).as(APIPagination) { pagination =>
        respondWithMediaType(`application/json`) {
          complete("things")
        }
      }
    } ~ 
    path(Segment) { thingStringId =>
      pathEnd {
        complete(thingStringId)
      } ~
      pathSuffix("subthings") {
        pathEndOrSingleSlash {
          complete("subthings")
        }
      } ~
      pathSuffix("othersubthings") {
        pathEndOrSingleSlash {
          complete("othersubthings")
        }
      } 
    }
  }
} ~ //more routes...
Run Code Online (Sandbox Code Playgroud)

这没有问题编译,但是当使用scalatest来验证路由结构是否正确时,我很惊讶地发现这种类型的输出:

"ThingServiceTests:"
"Thing Service Routes should not reject:"
- should /v0/things
- should /v0/things/thingId
- should …
Run Code Online (Sandbox Code Playgroud)

scala spray spray-dsl

2
推荐指数
1
解决办法
1972
查看次数

标签 统计

scala ×2

spray ×2

spray-dsl ×1