我需要使用spray框架发出一个简单的HTTP请求.我在他们的网站上发现了一些例子,但事实证明它们很复杂并且涉及Akka,这对我来说并不是必需的.
此外,我需要能够填写请求的标题(如X-Application,content-type等),当然还有请求的发布数据(在我的情况下,它将是JSON中的数据).
那我该怎么做?
我正在看由eigengo编写的Typesafe的Activator中的“ Akka and Spray ”教程的代码。我没有定义jsonFormat1,jsonFormat2,...的位置以及jsonFormatN的工作方式。
implicit val sendMessageFormat = jsonFormat2(SendMessage)
上面的代码片段在scala> api> MessengerService.scala中
谢谢。
我有以下代码,它使用spray-json通过该parseJson方法将一些JSON反序列化为case类.
根据隐式JsonFormat [MyCaseClass]的定义位置(在线或从伴随对象导入),以及在定义时是否提供显式类型,代码可能无法编译.
我不明白为什么从配对对象导入隐式需要它在定义时具有显式类型,但如果我把它内联,那不是这种情况?
有趣的是,IntelliJ在所有情况下都正确地定位了隐式参数(通过cmd-shift-p).
我正在使用Scala 2.11.7.
Broken Code - 来自伴侣对象的通配符导入,推断类型:
import SampleApp._
import spray.json._
class SampleApp {
import MyJsonProtocol._
val inputJson = """{"children":["a", "b", "c"]}"""
println(s"Deserialise: ${inputJson.parseJson.convertTo[MyCaseClass]}")
}
object SampleApp {
case class MyCaseClass(children: List[String])
object MyJsonProtocol extends DefaultJsonProtocol {
implicit val myCaseClassSchemaFormat = jsonFormat1(MyCaseClass)
}
}
Run Code Online (Sandbox Code Playgroud)
结果是:
Cannot find JsonReader or JsonFormat type class for SampleAppObject.MyCaseClass
Run Code Online (Sandbox Code Playgroud)
请注意,显式导入myCaseClassSchemaFormat隐式时会发生同样的事情.
工作代码#1 - 来自伴侣对象的通配符导入,显式类型:
将显式类型添加到随播对象中的JsonFormat会导致代码编译:
import SampleApp._
import spray.json._
class SampleApp {
import MyJsonProtocol._
val inputJson = """{"children":["a", …Run Code Online (Sandbox Code Playgroud) SBT使用specs2和spray-testkit在我的一些较大的验收风格测试中保持内存不足.我有10个演出或RAM可用,目前我开始SBT(使用SBT附加脚本),MaxPermSize为512m,Xms为1024m,Xmx为2g.
验收测试按特定顺序贯穿客户的整个业务流程,因此将验收测试分成多个较小的测试并不容易.
任何想法如何更好地配置我的环境,或者我应该注意的问题将不胜感激.
为了它的价值,我在Ubuntu下使用Oracle Java,该项目使用Scala 2.10,sbt 0.12.2,使用specs2 1.14喷涂1.1-M7.
在测试之外运行系统时,或者在使用较小的测试时,一切都像发条一样运行.只有在较大的测试中,事情才会变得坚果.
文档说明喷雾能够处理分块响应,但我找不到任何开始的例子.有我天真的实施:
object Main extends App {
implicit val system = ActorSystem()
import system.dispatcher
val log = Logging(system, getClass)
val ioBridge = IOExtension(system).ioBridge()
val httpClient = system.actorOf(Props(new HttpClient(ioBridge)))
val conduit = system.actorOf(
props = Props(new HttpConduit(httpClient, "localhost", 3000)),
name = "http-conduit"
)
val pipeline = HttpConduit.sendReceive(conduit)
val response = pipeline(
HttpRequest(
method = GET,
uri = "/output.cgi.xml"
)
)
response onComplete {
case Success(a) =>
log.info("Success: " + a)
system.shutdown()
case Failure(error) =>
log.error(error, "Failure")
system.shutdown()
}
}
Run Code Online (Sandbox Code Playgroud)
我已经设定 …
有没有人根据以下两种组合对他/她的应用程序的性能进行基准测试?
我认为2)在大多数情况下表现优于1),即使1)使用servlet 3.0功能.
我之所以要问的是,我的团队需要权衡性能和应用程序部署/管理(自动扩展,监控等)的简易性,因为AWS Elastic Beanstalk的默认java webapp配置是运行Tomcat的Linux.
对此的任何意见将非常感激.干杯
我应该使用play或spray来获得高效的高吞吐量REST json服务器吗?我会继续玩,因为这样我就可以拥有一个用于REST和Web开发的单一框架.然而,如果喷雾比这更有效,我会用喷雾.
我会马上去玩,但我在这里看到喷雾在列表中很高,并且游戏更接近底部.
你怎么看?
MyService.scala:33: could not find implicit value for parameter eh: spray.routing.ExceptionHandler
我在使用Akka时遇到了"缺少隐式"的编译错误,在spray.io代码中,对一个单独的后端服务器进行http调用,作为响应http get的一部分.代码需要导入相当多的Spray和Akka库,因此有点难以确定是否存在导致此问题的库冲突,我宁愿想出如何在这个和其他情况下逻辑地跟踪这类问题.
在调用时遇到缺少的隐式 runRoute(myRoute)
这是代码:
import spray.routing._
import akka.actor.Actor
import akka.actor.ActorSystem
import spray.http._
import MediaTypes._
import akka.io.IO
import spray.httpx.RequestBuilding._
import scala.concurrent.Future
import spray.can.Http
import spray.http._
import akka.util.Timeout
import HttpMethods._
import akka.pattern.ask
import akka.event.Logging
import scala.concurrent.duration._
// we don't implement our route structure directly in the service actor because
// we want to be able to test it independently, without having to spin up an actor
class MyServiceActor extends Actor with …Run Code Online (Sandbox Code Playgroud) 我有这条路线:
val routes =
pathPrefix("api") {
path("ElevationService" / DoubleNumber / DoubleNumber) { (long, lat) =>
post {
requestContext =>
println(long, lat)
}
}
}
Run Code Online (Sandbox Code Playgroud)
这很好用,我可以称之为ElevationService:
http://localhost:8080/api/ElevationService/39/80
Run Code Online (Sandbox Code Playgroud)
问题是,我还想在请求中解析发送给我的身体为JSON.它看起来如下:
{
"first": "test",
"second": 0.50
}
Run Code Online (Sandbox Code Playgroud)
我已经设法让它在一个单独的路径中工作,遵循实体指令的文档:
path("test") {
import scrive.actors.ScriveJsonProtocol
import spray.httpx.SprayJsonSupport._
post {
entity(as[ScriveRequest]) { scrive =>
complete(scrive)
}
}
}
Run Code Online (Sandbox Code Playgroud)
但我不知道如何将这两条路线合并为一条路线.因为它们包含在函数中,所以我不能在函数内调用参数long,我认为它们不存在于该范围内.相同或相反.latentity
我希望能够访问我的params和我的POST主体,然后调用传递所有数据的服务:
val elevationService = actorRefFactory.actorOf(Props(new ElevationService(requestContext)))
elevationService ! ElevationService.Process(long, lat, bodyParams)
Run Code Online (Sandbox Code Playgroud) 我使用以下代码将一些数据发布到Server
def post(endpoint: String, entity: Strict) = {
Http().singleRequest(HttpRequest(uri = Notifier.notificationUrl + endpoint, method = HttpMethods.POST,
entity = entity)) onComplete {
case Success(response) => response match {
case HttpResponse(StatusCodes.OK, _, _, _) =>
log.info("communicated successfully with Server")
}
case Failure(response) =>
log.error("communicated failed with Server: {}", response)
}
}
Run Code Online (Sandbox Code Playgroud)
10 seconds当Notifieractor接收到如下消息时,每次调用此方法
case ecMonitorInformation: ECMonitorInformation =>
post("monitor", httpEntityFromJson(ecMonitorInformation.toJson))
Run Code Online (Sandbox Code Playgroud)
问题?
我看到最初(围绕5请求转到服务器)然后它挂起,我没有看到任何日志记录,服务器没有收到任何数据.在客户端一段时间后,我看到了以下内容
ERROR c.s.e.notification.Notifier - communicated failed with Server: java.lang.RuntimeException: Exceeded configured max-open-requests value of [32]
Run Code Online (Sandbox Code Playgroud)
到底是怎么回事?我该如何解决这个问题?
spray ×10
scala ×6
akka ×3
json ×3
spray-json ×2
akka-http ×1
deployment ×1
http ×1
implicit ×1
marshalling ×1
performance ×1
rest ×1
sbt ×1
specs2 ×1
spray-dsl ×1
tomcat ×1