Spray(7)REST API Project - Routing

本文介绍了一个使用 Spray 框架实现的 RESTful API 服务,重点讲解了如何通过 URL 路由Actor来管理不同的资源路径,并展示了具体的路由配置示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spray(7)REST API Project - Routing

5. How to deal with routing
First start class Boot.scala
package com.sillycat.easysprayrestserver

import spray.can.server.SprayCanHttpServerApp
import akka.actor.Props
import com.sillycat.easysprayrestserver.bootstrap.Bootstrap
import com.sillycat.easysprayrestserver.actor.URLRouterActor

object Boot extends App with Bootstrap with SprayCanHttpServerApp {

valhandler = system.actorOf(Props[URLRouterActor])

newHttpServer(handler) ! Bind(interface = serverAddress, port = serverPort)


}

Bootstrap.scala to prepare some parameters
package com.sillycat.easysprayrestserver.bootstrap

import akka.actor.ActorSystem
import com.typesafe.config.ConfigFactory

trait Bootstrap {

//implicit val system = ActorSystem("RESTAPIService")

valconfig = ConfigFactory.load()

valenv = config.getString("build.env")
varserverAddress: String = config.getString("server.address")
varserverPort: Int = config.getInt("server.port")

if (env != null && env != "") {
serverAddress = if (config.getString("environment." + env + ".server.address") != null) config.getString("environment." + env + ".server.address") elseserverAddress
serverPort = if (config.getInt("environment." + env + ".server.port") != 0) config.getInt("environment." + env + ".server.port") elseserverPort
}


}

URLRouterActor to manage the URL resources
package com.sillycat.easysprayrestserver.actor

import akka.actor.{ Props, Actor }
import spray.routing._
import spray.routing.directives._
import spray.util.LoggingContext
import spray.http.StatusCodes._
import spray.httpx.SprayJsonSupport._
import shapeless._

class URLRouterActor extends Actor with URLRouterService {
def actorRefFactory = context
def receive = runRoute(route)
}

trait URLRouterService extends HttpService {
def route = {
pathPrefix(Version / BrandCode) { (apiVersion, brandCode) =>

path("resource" / "all") {
get {
complete {
"Morning, guest. apiVersion = " + apiVersion + ", brandCode =" + brandCode
}
}
} ~
path("resource" / "admin-only") {
get {
complete {
"Morning, admin-only. apiVersion = " + apiVersion + ", brandCode =" + brandCode
}
}
} ~
path("resource" / "customer-only") {
get {
complete {
"Morning, customer-only. apiVersion = " + apiVersion + ", brandCode =" + brandCode
}
}
} ~
path("resource" / "better-admin") {
get {
complete {
"Morning, better-admin. apiVersion = " + apiVersion + ", brandCode =" + brandCode
}
}
} ~
path("resource" / "better-customer") {
get {
complete {
"Morning, better-customer. apiVersion = " + apiVersion + ", brandCode =" + brandCode
}
}
}
}
}

valVersion = PathMatcher("""v([0-9]+)""".r)
.flatMap {
casestring :: HNil => {
try Some(java.lang.Integer.parseInt(string) :: HNil)
catch {
case _: NumberFormatException => None
}
}
}

valBrandCode = PathElement

}

Visit the URL as follow to verify that.
http://localhost:9000/v1/sillycat/resource/better-customer

6. How to Deal with Auth


7. How to work with DB
come soon...


8. How to Work with Actor
come soon…


9. How to do Validation
come soon...


10. How to Work with Logback
come soon…


Tips:
1. Add formatter to plugins
project/plugins.sbt
addSbtPlugin("com.typesafe.sbt" % "sbt-scalariform" % "1.0.1")


References:
http://www.gtan.com/akka_doc/scala/routing.html
https://github.com/cakesolutions/spray-auth-example
http://spray.io/documentation/spray-routing/
https://github.com/spray/spray/wiki/Authentication-Authorization
https://github.com/spray/spray/wiki/Configuration
https://github.com/spray/spray/wiki
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值