考慮一個HTTP服務,可以返回兩個JSON作爲響應:如何在噴霧中編組不同的響應類型?
成功
失敗
{
"errorCode": 3
}
爲了解決這些JSON的,我需要創建2 case類case class RespSucc(yourField:String)
和 case class RespFail(errorCode:Int)
。
現在我必須要這樣的事情:
//unmarshal is spray.httpx.ResponseTransformation#unmarshal
if (response.entity.asString.contains("errorCode")) {
unmarshal[RespSucc].apply(response)
}
else {
unmarshal[RespFail].apply(response)
}
是否有一個API來自動解析這些類沒有任何if
?例如。 unmarshaller可以查看json字段並選擇合適的case類嗎?