I'm trying to ask consul for healthy services.The response is:
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}]}]
The class definitions are:
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 {}
Then I try to unmarshall:
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]]
and get the following error:
Error during processing of request: 'Attempt to decode value on failed cursor: DownField(node),DownArray'. Completing with 500 Internal Server Error response. To change default exception handling behavior, provide a custom ExceptionHandler.
Ican't see the failure, anybody else can do?
The short answer: Assuming you have all the required decoders and encoders in place, you should simply fix your case class as follows:
case class Node (
Node: String,
Address: String,
TaggedAddresses: TaggedAddresses,
CreateIndex: Int,
ModifyIndex: Int
)
I.e., you have to use the tag names exactly as they appear in your JSON.
The long answer: if I'm taking out the relevant JSON from your object as follows:
val jsonString =
"""
{
"Node":{
"Node":"ci-content-1",
"Address":"10.45.200.14",
"TaggedAddresses":{
"wan":"10.45.200.14"
},
"CreateIndex":2708577,
"ModifyIndex":3470978
},
...
"""
Then the following code will yield Right(Node(ci-content-1,10.45.200.14,TaggedAddresses(10.45.200.14),2708577,3470978))
, when the above, corrected version of the case class is used:
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)
}
Otherwise, I also get the same error you described.