getOrElse Scala播放JSON Payload

时间:2015-08-06 18:41:06

标签: json scala playframework

我试图不仅清理这段代码,而且在request.body isEmpty时它会破坏,这会破坏这段代码的目的。我一直在参考Play的文档来实现getOrElse,但我很难看到如何将我的代码段转换为他们提供的内容。这是方法:

  override def createWOTC(clientID: Int) =
Action(parse.json) { implicit request =>
  val partnerID = {
    if ((request.body \ "partner_id").as[String].isEmpty) "jobapp"
    else (request.body \ "partner_id").as[String]
  }
  val partnerAuth = {
    if ((request.body \ "partner_auth").as[String].isEmpty) "(snip)"
    else (request.body \ "partner_auth").as[String]
  }
  val handshakeURL = {
    if ((request.body \ "handshake_url").as[String].isEmpty) "(snip)"
    else (request.body \ "handshake_url").as[String]
  }
    ...


  wotcService.createWOTC(clientID, (request.body \ "company").as[String], (request.body \ "auth").as[String],
    partnerID, partnerAuth, handshakeURL, eligibilityURL, hireURL, pdfURL)
  NoContent
}

1 个答案:

答案 0 :(得分:2)

val partnerAuth = (request.body \ "partner_auth").asOpt[String].getOrElse("SxtWJbboccljiaii")