【发布时间】:2020-03-24 22:16:08
【问题描述】:
我正在开发 scala play 框架应用程序。我正在尝试调用一个 Web 服务 API,该 API 获取请求有效负载数据,如下所示
{
"toID": [
"email1@email.com",
"email2@email.com"
],
"fromID": "info@test.com",
"userID": "ervd12fdsfksdjnfn9832rbjfdsnf",
"mailContent": "Dear Sir, ..."
}
为此,我使用以下代码
ws.url(Utils.messengerServiceUrl + "service/email")
.post(
Map("userID" -> userID, "mailContent" -> userData.message, "fromID" -> "info@test.com", "toID" -> userData.emails)).map { response =>
println(response.body, response.status)
}
所以对于这段代码,编译器抱怨 "toID" -> userData.emails 说 No implicits found for parameter evidence$2: BodyWritable[Map[String, Object]]强>
所以我的问题是如何使用 WSClient 发送此类数据?
【问题讨论】:
-
userData.emails是什么类型的? -
userData.emails 的类型是 Seq[String]
-
为什么要把它作为地图发送?您可以创建描述有效负载的案例类,例如
case class Message(toID: Seq[String], fromID: String, userID: String, mailContent: String) -
我是 scala play 框架的新手,这是我知道发送有效负载数据的唯一方法。我创建了案例类并尝试用 Message(userID, userData.message, "info@test.com", userData.emails) 替换 Map,但随后它显示类似 BodyWritable [Message] 的错误。那么您能否给我一个发送案例类对象作为有效负载数据的示例。
-
您需要为该案例类
Writes[Message]指定 json 编码器并隐式传递它,例如implicit val messageWrites = Json.writes[Message]
标签: java scala rest functional-programming playframework-2.6