POST请求有时会在Akka-http程序中遇到错误的端点

时间:2016-01-22 09:19:54

标签: scala alamofire spray akka-http

我有一个Akka-http 2服务器来处理HTTP请求。

在任一端点上,我都转换为案例类A或B:

case class caseClassA(data: String, eventID: String)
case class caseClassB(data: String, otherData: JsObject)

我有一个像这样的路线对象:

   val routes = {
     logRequestResult("akka-http-microservice") {        
       pathPrefix("some_endpoint")
        (post & entity(as[caseClassA])) {
        Request =>
          val future = someFunction(Request)
            complete {
              future.map(result =>
                result
              )
            }
        } ~
        pathPrefix("other_endpoint") {
          (post & entity(as[caseClassB])) {
          Request =>
            val future = otherFunction(Request)
              complete {
                future.map(result =>
                  result
                ) 
              }
            }
          }
        }

问题 有时 ,当使用Alamofire从iOS应用程序调用时,我收到错误数据的错误响应{ {1}}在使用caseClassB-convertible JSON对象发出some_endpoint请求时:

other_endpoint

正如您在缺少[DEBUG] [01/22/2016 16:30:16.350] [ReactiveKafka-akka.actor.default-dispatcher-23] [ActorSystem(ReactiveKafka)] akka-http-microservice: Response for   Request : HttpRequest(HttpMethod(POST),http://192.168.2.141:9004/other_endpoint,List(Host: 192.168.2.141:9004, Connection: keep-alive, Accept: */*, User-Agent: myapp/myco.myapp (1; OS Version 9.2  (Build 13C75)), Accept-Language: en-US, zh-Hans-US;q=0.9, Accept-Encoding: gzip, compress;q=0.5),HttpEntity.Strict(application/json,ByteString(XXX, XXX, XXX, XXX, ETC <REPLACED FOR READABILITY, THIS WAS GOOD DATA>)),HttpProtocol(HTTP/1.1))   Response: Complete(HttpResponse(200 OK,List(),HttpEntity.Strict(application/json,ByteString(XXX, XXX, XXX, XXX, ETC <REPLACED FOR READABILITY, THIS WAS GOOD DATA>)),HttpProtocol(HTTP/1.1))) [DEBUG] [01/22/2016 16:30:21.347] [ReactiveKafka-akka.actor.default-dispatcher-5] [akka://ReactiveKafka/user/$a/flow-17-2-prefixAndTail] Cancelling akka.stream.impl.MultiStreamOutputProcessor$SubstreamOutput@454147bb (after: 5000 ms) [DEBUG] [01/22/2016 16:30:26.259] [ReactiveKafka-akka.actor.default-dispatcher-7] [ActorSystem(ReactiveKafka)] akka-http-microservice: Response for   Request : HttpRequest(HttpMethod(POST),http://192.168.2.141:9004/other_endpoint,List(Host: 192.168.2.141:9004, Connection: keep-alive, Accept: */*, User-Agent: myapp/myco.myapp (1; OS Version 9.2  (Build 13C75)), Accept-Language: en-US, zh-Hans-US;q=0.9, Accept-Encoding: gzip, compress;q=0.5),HttpEntity.Default(application/json,319,akka.stream.scaladsl.Source@c3e53bd),HttpProtocol(HTTP/1.1))   Response: Rejected(List(MalformedRequestContentRejection(Object is missing required member 'eventID',Some(java.util.NoSuchElementException: key not found: eventID)), TransformationRejection(<function1>), MalformedRequestContentRejection(Unexpected end-of-input at input index 0 (line 1, position 1), expected JSON Value: ^ ,None), TransformationRejection(<function1>))) 时所看到的,这是尝试转换为caseClassA。上面输出中的两个请求都显示我正在尝试触及other_endpoint。当我使用相同的数据从Postman调用此端点时,它可以100%的时间工作。

是什么给出了?

1 个答案:

答案 0 :(得分:0)

如上面的评论所示,问题是{}的代码周围缺少大括号("some_endpoint")。这是更正后的代码块:

val routes = {
  logRequestResult("akka-http-microservice") {        
    pathPrefix("some_endpoint") {
      (post & entity(as[caseClassA])) {
         Request =>
         val future = someFunction(Request)
         complete {
           future.map(result =>
             result
           )
         }
       }
     } ~
     pathPrefix("other_endpoint") {
       (post & entity(as[caseClassB])) {
         Request =>
         val future = otherFunction(Request)
         complete {
           future.map(result =>
             result
           ) 
         }
       }
     }
   }