我正在使用spray-json将自定义对象列表编组到JSON中.我有以下case类和它的JsonProtocol.
case class ElementResponse(name: String, symbol: String, code: String, pkwiu: String, remarks: String, priceNetto: BigDecimal, priceBrutto: BigDecimal, vat: Int, minInStock:Int, maxInStock: Int)
object JollyJsonProtocol extends DefaultJsonProtocol with SprayJsonSupport {
implicit val elementFormat = jsonFormat10(ElementResponse)
}
Run Code Online (Sandbox Code Playgroud)
当我试图像这样的路线投入时:
get {
complete {
List(new ElementResponse(...), new ElementResponse(...))
}
}
Run Code Online (Sandbox Code Playgroud)
我得到一个错误说:
could not find implicit value for evidence parameter of type spray.httpx.marshalling.Marshaller[List[pl.ftang.scala.polka.rest.ElementResponse]]
Run Code Online (Sandbox Code Playgroud)
也许你知道这是什么问题?
我正在使用Scala 2.10.1喷雾1.1-M7和喷雾-json 1.2.5
我有一个基于actor的系统,它执行定期的,CPU密集的数据摄取以及RESTful端点.我正在使用Akka演员来发信号/控制摄取过程的各个阶段,而Spray(当然是建立在Akka上)来为我的休息端点提供服务.
我的问题是这样的:当摄取开始时它消耗了大部分CPU,使RESTful端点挨饿直到完成.
降低摄取优先级的最佳方法是什么?现在,摄取和Spray模块共享相同的ActorSystem,但如果这有助于解决方案,它们可以分开.
使用spray的流水线操作来发出这样的HTTP请求时:
val urlpipeline = sendReceive ~> unmarshal[String]
urlpipeline { Get(url) }
Run Code Online (Sandbox Code Playgroud)
有没有办法为请求指定超时以及它应该为该特定请求重试的次数?
我发现的所有文档只引用了一个配置(即便如此,我似乎无法让它工作).
谢谢
我on_spray-can_1.1按照Spray的" 入门 "页面上的建议克隆了Spray模板项目(分支),并使用sbt-idea生成相应的IDEA项目.它建立得很好,但是当我打开我的build.sbt文件时,最后一行让IDEA不高兴:
seq(Revolver.settings: _*)
Run Code Online (Sandbox Code Playgroud)
它不承认Revolver,并建议进口spray.revolver.RevolverPlugin.Revolver,这似乎是合理的.但是,当我这样做时,它仍然抱怨"表达式类型(Def.SettingsDefinition)必须符合SBT文件中的设置[_]".
显然,这不是一个真正的问题,或者IDEA的(外部)make会失败,和/或SBT会从命令行中抱怨.但为什么IDEA认为这是一个问题?最近版本的SBT有什么变化吗?
当我看到SBT 0.13.0源代码时,我注意到seq它已被弃用; 它说"在build.sbt文件中,可以删除此调用." 但是,如果我这样做,Revolver会停止工作("不是有效命令:重新启动").
FWIW,我在Scint 2.10.3上使用SBT 0.13.0,在Mint 14上使用IDEA 12.1.6.
我有以下服务:
trait PingService extends MyHttpService {
val pingRoutes =
path("ping") {
get {
complete("message" -> "pong")
}
}
}
Run Code Online (Sandbox Code Playgroud)
MyHttpService是一个扩展的自定义类,HttpService只包含实用程序方法.
这是测试规范:
import akka.actor.ActorRefFactory
import org.json4s.{DefaultFormats, Formats}
import org.scalatest.{FreeSpec, Matchers}
import spray.testkit.ScalatestRouteTest
class PingServiceSpec extends FreeSpec with PingService with ScalatestRouteTest with Matchers {
override implicit def actorRefFactory: ActorRefFactory = system
override implicit def json4sFormats: Formats = DefaultFormats
"Ping service" - {
"when calling GET /ping" - {
"should return 'pong'" in {
Get("/ping") ~> pingRoutes ~> check { …Run Code Online (Sandbox Code Playgroud) 我试图在scala中使用spray-json来识别转换为Json和返回时Ec2Provider和OpenstackProvider之间的选择.我希望能够在"提供者"中做出选择,如果这些选择不符合可用的选择,那么它就不应该验证.
我在这方面的尝试可以在以下代码中看到:
import spray.json._
import DefaultJsonProtocol._
case class Credentials(username: String, password: String)
abstract class Provider
case class Ec2Provider(endpoint: String,credentials: Credentials) extends Provider
case class OpenstackProvider(credentials: Credentials) extends Provider
case class Infrastructure(name: String, provider: Provider, availableInstanceTypes: List[String])
case class InfrastructuresList(infrastructures: List[Infrastructure])
object Infrastructures extends App with DefaultJsonProtocol {
implicit val credFormat = jsonFormat2(Credentials)
implicit val ec2Provider = jsonFormat2(Ec2Provider)
implicit val novaProvider = jsonFormat1(OpenstackProvider)
implicit val infraFormat = jsonFormat3(Infrastructure)
implicit val infrasFormat = jsonFormat1(InfrastructuresList)
println(
InfrastructuresList(
List(
Infrastructure("test", Ec2Provider("nova", Credentials("user","pass")), List("1", "2")) …Run Code Online (Sandbox Code Playgroud) 我知道喷雾为我做了这个,但我仍然希望用我的标题覆盖它,我怎样才能覆盖响应中的标题?
我的回答如下:
case HttpRequest(GET, Uri.Path("/something"), _, _, _) =>
sender ! HttpResponse(entity = """{ "key": "value" }""" // here i want to specify also response header i would like to explicitly set it and not get it implicitly
Run Code Online (Sandbox Code Playgroud) 在更新到喷雾1.2后,我遇到了一个与我的JSON-Marshallers完全兼容的问题.在HttpService中执行以下操作
trait TestHttpService extends HttpService with SprayJsonSupport with DefaultJsonProtocol{ self : ActorLogging =>
case class Test(hallo: String, test: String)
implicit val storyJsonFormat = jsonFormat2(Test.apply)
def test(implicit m : Marshaller[Future[Test]]) = 17
def hallo = test
}
Run Code Online (Sandbox Code Playgroud)
导致以下错误:
could not find implicit value for parameter marshaller:
spray.httpx.marshalling.Marshaller[scala.concurrent.Future[amanuensis.story.Story]]
Run Code Online (Sandbox Code Playgroud)
当我删除未来时,一切运作良好:
trait TestHttpService extends HttpService with SprayJsonSupport with DefaultJsonProtocol { self : ActorLogging =>
case class Test(hallo: String, test: String)
implicit val storyJsonFormat = jsonFormat2(Test.apply)
def test(implicit m : Marshaller[Test]) = 17
def hallo …Run Code Online (Sandbox Code Playgroud) 我有一个POST Spray路由,请求包含一个JSON主体(内容类型"application/json").我想要一种方法从我的路线中的这个请求中提取原始JSON.
对于http:// host:port/somepath/value1我想将post体提取为TextMsgResponse.但是对于http:// host:port/somepath/value2我想提取 post体就像一个原始的Json(例如,{ "name":"Jack", "age":30 }
val myRoute = path("somepath" / Segment) { pathSegment =>
post { //use only POST requests
pathSegment match {
case "value1" =>
entity(as[TextMsgResponse]) { textMsg =>
complete {
//do something with the request
StatusCodes.OK
}
}
case "value2" => {
//here is I want to extract the RAW JSON from the request
}
}
}
Run Code Online (Sandbox Code Playgroud) 是否有可能在Akka(scala)中获得对现有ActorSystem的引用?
我正在与另一个DB的Actor一起开发Spray应用程序.我也在扩展Directives以获得每个路径的对象.指令本身不是actor,但是它们需要将消息传递给DBActor.这里:
class HttpActor extends Actor with HttpService {
val actorRefFactory = context
def receive = runRoute(
IndexService.route ~
HostsService.route
)
}
object HostsService extends Directives{
def route(implicit dm: DetachMagnet2) = {
path("hosts") {
get {
detach() {
**dbActor ! CreateHost**
complete("get me hosts!")
}
} ~
post {
detach() {
entity(as[String]) { payload =>
complete(s"post hosts $payload")
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法让HostsService发现ActorSystem本身,这样他就可以找到DBActor,或者HttpActor是否必须将其传入?后者似乎不太优雅,因为HostsService需要成为一个类(而不是一个对象),因此不再是单例.
spray ×10
scala ×9
akka ×5
json ×2
spray-json ×2
actor ×1
future ×1
pipeline ×1
post ×1
rest ×1
sbt ×1
scala-2.10 ×1
spray-client ×1
testing ×1