我正在向领事询问健康的服务。回复是:
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(节点),DownArray'。完成500内部服务器错误响应。要更改默认的异常处理行为,请提供自定义的ExceptionHandler。
我看不到失败,其他任何人都可以做到?
答案 0 :(得分: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)
}
否则,我也会得到您描述的相同错误。