Scala Akka HTTP 访问 post 请求参数时出现问题

问题描述 投票:0回答:1

我正在尝试使用 Akka HTTP Server 和 Scala 编程语言创建 Post 请求路径。

但是当我成功发出请求后,我发现很难访问请求体参数的值。

我想访问参数

h
、Impression 的
w
和 Site 的
domain
的值。

访问这些参数的最佳方式是什么?

我使用

SprayJSON
作为编组器。

当前代码

val h =bid_request_params.imp.h
给出错误
value h is not a member of Option[List[com.example.WebServer.Impression]]

再次,请求已成功处理,我只是不知道如何访问参数的值。

代码如下。

案例类别:


case class Site(id: String, domain: String)

case class Impression(id: String, h: Int, w: Int)

case class MyRequest(id: String, imp: Option[List[Impression]], site: Site)

Post请求路径:


path("submit") {
   post {
    entity(as[MyRequest]) { bid_request_params =>
    val h = bid_request_params.imp.h
    val w = bid_request_params.imp.w
    val w = bid_request_params.site.domain
    }
  }
}

请求正文:


{
 "id": "SGu1Jpq1IO",
 "site": {
       "id": "0006a522f",
       "domain": "btmn.tld"
  },
 "imp": [{
       "id": "1",
       "h": 250,
       "w": 300
  }]
}

scala sbt akka akka-http
1个回答
0
投票

您遇到的问题是如何尝试访问

imp
类型的
Option[List[Impression]]
字段的值。

根据您定义案例类的方式,如果您尝试手动创建

MyRequest

的实例,您将得到类似的结果
case class Site(id: String, domain: String)
case class Impression(id: String, h: Int, w: Int)
case class MyRequest(id: String, imp: Option[List[Impression]], site: Site)

val request = MyRequest(
  id = "SGu1Jpq1IO",
  site = Site(
    id = "0006a522f",
    domain = "btmn.tld"
  ),
  imp = Some(       // here you have an Option
    List(           // here you have the List
      Impression(   // here is the Impression class you define
        id = "1",
        h = 250,
        w = 300
      )
    )
  )
)

当你尝试做类似的事情时

val h = request // MyRequest
          .imp  // this field is of type Option[List[Impression]], not just Impression
          .h    // can't solve this because of the previous line
val w = request.imp.w
val w = request.site.domain

Option
是一个可让您处理当前值或非当前值概念的类。需要使用该类提供的方法。大多数是
map
flatMap
filter
,但也有其他如
fold
getOrEsle
orElse
,甚至您可以使用
for yield
pattern matching

同样的情况也发生在

List
上,如果不使用允许您遍历集合并对列表中包含的值执行某些操作的方法,则无法直接访问元素的字段。

话虽这么说,不确定您是否需要重新定义有效负载的架构,或者您需要调整代码以对端点中收到的值执行某些操作。

这里有一些代码示例

bid_request_params.imp match {
  case Some(imps) => // in case the list was sent in the payload and is not null
    for {
      imp <- imps
    } yield imp.h + imp.w // just doing a sum that probably doesn't make sense. Just to show that at this point I have access to the fields and this will return a new List
  case None => // the list was not sent in the payload 
}

// the following example should do the same of the code above but written in different way
bid_request_params
  .imp                 // this is an Option
  .map(imps =>         // the method from Option that lets me apply some transformation of the value contained, in this case the List
    imps
      .map(imp =>      // the method from List that lets me apply some transformation of each element of the List, in this case the Impressions
        imp.h + imp.w  // again the non-sense sum of the fields 
      )
  )
© www.soinside.com 2019 - 2024. All rights reserved.