文档说明喷雾能够处理分块响应,但我找不到任何开始的例子.有我天真的实施:
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)
我已经设定 …
抱歉模糊的标题...不知道如何表征这一点.
我已经在Scala中看到/使用过某种代码构造已有一段时间但我不知道它是如何工作的.它看起来像这样(来自Spray路由的示例):
path( "foo" / Segment / Segment ) { (a,b) => { // <-- What's this style with a,b?
...
}}
Run Code Online (Sandbox Code Playgroud)
在此示例中,路径中的Segements分别绑定到关联块内的a和b.我知道如何使用这种模式,但它是如何工作的?为什么它没有绑定到"foo"?
我不太喜欢喷雾如何为我的目的而工作,但Scala的设施是什么,我怎么写自己的?
我想支持提交到同一网址的几种不同的内容类型:
例如:
application/x-www-form-urlencoded,multipart/form-data,application/json
我想做的事情如下:
post {
contentType(`application/x-www-form-urlencoded`) |
contentType(`multipart/form-data`) {
// user POSTed a form
entity(as[MyCaseClass]) { data =>
complete { data.result }
}
} ~ contentType(`application/json`) {
// user POSTed a JSON object
entity(as[MyCaseClass]) { data =>
complete { data.result }
}
}
}
Run Code Online (Sandbox Code Playgroud)
我认为可能有一些方法可以使用自定义编组和解组,但我只需要在我的服务中的一两个位置,这看起来很简单.有人可以帮忙吗?
我正试图重复这个或这个,但我一直得到一个错误,我无法解决...
首先,这是我的依赖:
compile 'io.spray:spray-can_2.11:1.3.1'
compile 'io.spray:spray-routing_2.11:1.3.1',
compile 'io.spray:spray-json_2.11:1.2.6'
Run Code Online (Sandbox Code Playgroud)
现在我要做的是:
class WHttpService extends Actor with HttpService with ActorLogging {
implicit def actorRefFactory = context
def receive = runRoute(route)
lazy val route = logRequest(showReq _) {
// Way too much imports but I tried all I could find
import spray.json._
import DefaultJsonProtocol._
import MasterJsonProtocol._
import spray.httpx.SprayJsonSupport._
path("server" / Segment / DoubleNumber / DoubleNumber) { (login, first, second) =>
get {
complete {
Answer(1, "test")
}
}
}
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试以下POC来检查如何获得高并发性
implicit def executionContext = context.system.dispatchers.lookup("async-futures-dispatcher")
implicit val timeout = 10 seconds
val contestroute = "/contestroute" {
get {
respondWithMediaType(`application/json`) {
dynamic {
onSuccess(
Future {
val start = System.currentTimeMillis()
// np here should be dealt by 200 threads defined below, so why
// overall time takes so long? why doesn't it really utilize all
// threads I have given to it? how to update the code so it
// utilizes the 200 threads?
Thread.sleep(5000)
val status = s"timediff ${System.currentTimeMillis() …Run Code Online (Sandbox Code Playgroud) 我正在尝试(并且失败)了解spray-json如何将json提要转换为对象.如果我有一个简单的键 - >值json feed然后它似乎工作正常但我想要读取的数据出现在这样的列表中:
[{
"name": "John",
"age": "30"
},
{
"name": "Tom",
"age": "25"
}]
Run Code Online (Sandbox Code Playgroud)
我的代码看起来像这样:
package jsontest
import spray.json._
import DefaultJsonProtocol._
object JsonFun {
case class Person(name: String, age: String)
case class FriendList(items: List[Person])
object FriendsProtocol extends DefaultJsonProtocol {
implicit val personFormat = jsonFormat2(Person)
implicit val friendListFormat = jsonFormat1(FriendList)
}
def main(args: Array[String]): Unit = {
import FriendsProtocol._
val input = scala.io.Source.fromFile("test.json")("UTF-8").mkString.parseJson
val friendList = input.convertTo[FriendList]
println(friendList)
}
}
Run Code Online (Sandbox Code Playgroud)
如果我更改我的测试文件,所以它只有一个人不在数组中运行val friendList = input.convertTo[Person]然后它工作,一切都解析但是一旦我尝试解析一个数组它失败并出现错误Object expected in …
我想我不能正确理解工作流程.我正在使用Apache Shiro和Stormpath在Scala中编写Web服务.我的用户身份验证过程如下所示:
1)从POST请求中获取用户数据,使用Stormpath进行检查,如果一切正常,请重定向到某个页面:
pathPrefix("api") {
path("login") {
post {
AuthToken.fromRequest { (token: AuthToken) =>
stormpathAuth(token) { subj =>
log.info("Subj {}", subj.getPrincipal.toString)
redirect("/some/page", StatusCodes.Found)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
在日志中没关系,Shiro用Stormpath帐户给我一个正确的主题.接下来我想提取主题,在代码中使用它:
pathPrefix("some") {
loggedInUser { subject =>
path("page") {
get {
complete {
html.render(Page.model)
}
}
} ..... other routes
Run Code Online (Sandbox Code Playgroud)
loggedInUser指令应提取主题并检查它是否经过身份验证,否则重定向到登录表单.问题是它总是将我重定向到登录表单,尽管在日志中SubjectUtils.getSubject.getPrincipal显示正确的帐户.
更新
实际上Spray是建立在Akka之上的.所以我认为问题落后于getSubject实现,目前依赖于ThreadLocal环境.我搜索了Shiro + Akka主题,但没有找到任何有用的信息.
如果我有一个端点解组json像这样:
(path("signup")& post) {
entity(as[Credentials]) { credentials =>
…
Run Code Online (Sandbox Code Playgroud)
如何使用Spray测试规范测试:
"The Authentication service" should {
"create a new account if none exists" in {
Post("/api/authentication/signup", """{"email":"foo", "password":"foo:" }""") ~> authenticationRoute ~> check {
handled === true
}
}
}
Run Code Online (Sandbox Code Playgroud)
由于几个原因,这显然不起作用.什么是正确的方法?
我有一个Spray服务,期望POST填写某些表单字段.我正在尝试研究如何在我的测试规范中创建一个合适的POST来测试它.
到目前为止我有什么
Post("/customer") ~> sealRoute(myRoute) ~> check {
responseAs[String] must contain("Success message")
}
Run Code Online (Sandbox Code Playgroud)
正如预期的那样,对/ customer路由进行POST.如何向此添加表单字段?
使用喷雾1.3.2和akka 2.3.6.(akka仅用于喷雾).
我需要读取大文件,并为每一行发出一个http请求.
我用迭代器逐行读取文件,并为每个项目发出请求.它成功地运行了一些线路,但在某些时候它开始失败:
akka.pattern.AskTimeoutException: Ask timed out on [Actor[akka://default/user/IO-HTTP#-35162984]] after [60000 ms].
我首先想到我重载了服务,所以我将"spray.can.host-connector.max-connections"设置为1.它运行得慢得多,但我得到了同样的错误.
这里的代码:
import spray.http.MediaTypes._
val EdnType = register(
MediaType.custom(
mainType = "application",
subType = "edn",
compressible = true,
binary = false,
fileExtensions = Seq("edn")))
val pipeline = (
addHeader("Accept", "application/json")
~> sendReceive
~> unmarshal[PipelineResponse])
def postData(data: String) = {
val request = Post(pipelineUrl).withEntity(HttpEntity.apply(EdnType, data))
val responseFuture: Future[PipelineResponse] = pipeline(request)
responseFuture
}
dataLines.map { d =>
val f = postData(d)
f.onFailure { case e => println("Error - …Run Code Online (Sandbox Code Playgroud) spray ×10
scala ×8
akka ×2
spray-json ×2
http ×1
json4s ×1
shapeless ×1
shiro ×1
specs2 ×1
spray-client ×1
spray-dsl ×1
spray-test ×1