0

我正在尝试向领事寻求健康的服务。答复是:

HttpResponse(200 OK,List(X-Consul-Index: 3471242, X-Consul-Knownleader: true, X-Consul-Lastcontact: 0, Date: Fri, 02 Mar 2018 16:06:08 GMT),HttpEntity.Strict(application/json,[{"Node":{"Node":"ci-content-1","Address":"10.45.200.14","TaggedAddresses":{"wan":"10.45.200.14"},"CreateIndex":2708577,"ModifyIndex":3470978},"Service":{"ID":"de62bdcb8e37:varnish_2:6085","Service":"shop-varnish-6085","Tags":null,"Address":"10.45.200.14","Port":33889,"EnableTagOverride":false,"CreateIndex":3313055,"ModifyIndex":3313055},"Checks":[{"Node":"ci-content-1","CheckID":"serfHealth","Name":"Serf Health Status","Status":"passing","Notes":"","Output":"Agent alive and reachable","ServiceID":"","ServiceName":"","CreateIndex":2708577,"ModifyIndex":3451134}]},{"Node":{"Node":"ci-content-2","Address":"10.45.200.18","TaggedAddresses":{"wan":"10.45.200.18"},"CreateIndex":2158463,"ModifyIndex":3471241},"Service":{"ID":"f89a94600d4c:varnish_1:6085","Service":"shop-varnish-6085","Tags":null,"Address":"10.45.200.18","Port":33622,"EnableTagOverride":false,"CreateIndex":3313064,"ModifyIndex":3313064},"Checks":[{"Node":"toom-ci-content-2","CheckID":"serfHealth","Name":"Serf Health Status","Status":"passing","Notes":"","Output":"Agent alive and reachable","ServiceID":"","ServiceName":"","CreateIndex":2158464,"ModifyIndex":3297480}]}]

类定义是:

final case class TaggedAddresses (
  wan: String)

final case class Node (
  node: String,
  address: String,
  taggedAddresses: TaggedAddresses,
  createIndex: Int,
  modifyIndex: Int
)

final case class Service (
  id: String,
  service: String,
  tags: String,
  addresses: String,
  port: Int,
  enableTagOverride: String,
  createIndex: Int,
  modifyIndex: Int
)

final case class Check (
  node: String,
  checkId:String,
  name: String,
  status: String,
  notes: String,
  output: String,
  serviceId: String,
  serviceName:String,
  createIndex: Int,
  modifyIndex: Int
)

final case class NodeInfo(
  node: Node,
  service: Service,
  checkList: List[Check]
)

package object VarnishInformation {}

然后我尝试解组:

val request = HttpRequest(method = HttpMethods.GET, uri = consulUrl)

val response = Await.result(Http().singleRequest(request), 10.seconds)

log.info("Entity: " + response.httpMessage)

val entries = Unmarshal(response).to[List[NodeInfo]]

并得到以下错误:

处理请求时出错:“尝试解码失败游标上的值:DownField(node),DownArray”。完成 500 内部服务器错误响应。要更改默认异常处理行为,请提供自定义 ExceptionHandler。

我看不到失败,其他人可以吗?

4

1 回答 1

2

简短的回答:假设您拥有所有必需的解码器和编码器,您应该简单地修复您的案例类,如下所示:

case class Node (
  Node: String,
  Address: String,
  TaggedAddresses: TaggedAddresses,
  CreateIndex: Int,
  ModifyIndex: Int
)

即,您必须完全按照它们出现在 JSON 中的方式使用标签名称。

长答案:如果我从您的对象中取出相关的 JSON,如下所示:

val jsonString = 
"""
{
   "Node":{
     "Node":"ci-content-1",
     "Address":"10.45.200.14",
     "TaggedAddresses":{
       "wan":"10.45.200.14"
     },
   "CreateIndex":2708577,
   "ModifyIndex":3470978
 },
 ...
"""

然后下面的代码将产生Right(Node(ci-content-1,10.45.200.14,TaggedAddresses(10.45.200.14),2708577,3470978)),当使用上面的案例类的更正版本时:

def myParse(jsonString: String) = {
  val res = parse(jsonString) match {
    case Right(json) => {
      val cursor = json.hcursor
      cursor.get[Node]("Node")
    }
    case _ => Left("Wrong JSON!") 
  }

  println(res)
}

否则,我也会收到您描述的相同错误。

于 2018-03-02T18:40:59.407 回答