0

我需要使用 Spray 发出发布请求,但我不明白如何在服务器端捕获请求。这是我的代码:

客户:

.
.
val pipeline: HttpRequest => Future[HttpResponse] = sendReceive

val fileName = "document.docx"
val path = getClass.getResourceAsStream("/test-documents/" + fileName)
val bytes = IOUtils.toByteArray(path)
val bytes64 = Base64.encodeBase64(bytes)
val streamInString = new String(bytes64)
val json: JsonInputStream = JsonInputStream(fileName, streamInString)
val jsonString = write(json)

val response: Future[HttpResponse] = pipeline(Post("http://localhost:8080/jsontest/", jsonString))

SERVER(4 个测试中的任何一个都没有捕获到请求):

startServer(interface = "localhost", port = 8080) {
    path("jsontest") {
      post {
        complete {
          <h1>TEST 1</h1>
        }
      }
    } ~
    path("jsontest" / Segment) { json =>
      post {
        complete {
          <h1>TEST 2</h1>
        }
      }
    } ~
    path("jsontest") {
      get {
        complete {
          <h1>TEST 3</h1>
        }
      }
    } ~
    path("jsontest" / Segment) { json =>
      get {
        complete {
          <h1>TEST 4</h1>
        }
      }
    }
  }

请帮帮我

4

2 回答 2

0

使用entity指令

看看http://spray.io/documentation/1.2-RC2/spray-routing/marshalling-directives/entity/#entity

application/json用于内容类型将是一个好主意

于 2013-11-05T12:25:15.220 回答
0

-.- 只需删除“/”"http://localhost:8080/jsontest/"

path("jsontest") {
  post {
    complete {
      <h1>TEST 1</h1>
    }
  }
}

捕捉它。对不起我的愚蠢

于 2013-11-05T12:18:36.553 回答