【发布时间】:2016-07-12 03:46:01
【问题描述】:
我正在使用 Play 2.5.x 来处理 HTTP 请求。发送到我的服务器的数据是 JSON 格式。所以 play 的 json 库就像import play.api.libs.json._ 一样使用。
我处理请求的操作如下:
def sendItems = Action(BodyParsers.parse.json) { req =>
val result = SMSItemObj.readItems(req.body)
result.fold(
errors => {
BadRequest(Json.obj("status" -> "KO", "message" -> JsError.toJson(errors)))
},
items => {
channel.basicPublish("", QUEUE_NAME, null, items.getBytes)
Ok(Json.obj("status" -> "OK"))
}
)
}
SMSItemObj 定义如下:
case class SMSItem(phone: String, content: String)
object SMSItemObj {
implicit val smsItemReads: Reads[SMSItem] = (
(JsPath \ "phone").read[String] and
(JsPath \ "content").read[String]
)(SMSItem.apply _)
def readItem(json: JsValue) = json.validate[SMSItem]
def readItems(json: JsValue) = json.validate[List[SMSItem]]
}
但是,items.getBytes 无法编译,看来 scala List 无法转换为 java 字节。
我正在使用amqp-client 3.6.1。
"com.rabbitmq" % "amqp-client" % "3.6.1"
【问题讨论】:
标签: json scala playframework rabbitmq playframework-2.5