Scala playframwork控制器返回已创建对象的ID

Scala playframwork控制器返回已创建对象的ID,scala,playframework,playframework-2.0,Scala,Playframework,Playframework 2.0,在scala playframework应用程序中,我希望返回我创建的项目的ID 控制器代码 def createClient = Action { implicit request => request.body.asJson.map(_.validate[ClientModel] match { case JsSuccess(client, _) => clientDTO.createClient(client).map{

在scala playframework应用程序中,我希望返回我创建的项目的ID

控制器代码

  def createClient = Action { implicit request =>
    request.body.asJson.map(_.validate[ClientModel] match {
      case JsSuccess(client, _) =>
        clientDTO.createClient(client).map{
          case cnt => println(cnt)
          case  _ => println("Fehler")
        }
      case err@JsError(_) => BadRequest("TEST")
      case _ => BadRequest("fail to create Counter")
    }).getOrElse(BadRequest("Failure tu create Counter"))
    Ok("s")
  }
DTO码

  /**
    * Insert Query for a new Client
    */
  val insertClientQuery = clients returning clients.map(_.id) into ((client, id) => client.copy(id = Some(id)))

  /**
    * Creates a new client
    *
    * @param client Client Model
    * @return
    */
  def createClient(client: ClientModel): Future[ClientModel] = {
    val action = insertClientQuery += client
    db.run(action)
  }
什么是对ID的最佳预操作,而不是返回OK

谢谢

错误

新错误

新的

又一次

类似的内容将返回一个JSON对象。您还可以潜在地将整个客户端作为JSON对象返回。此外,在本例中,创建的HTTP 201是一个更正确的响应

def createClient = Action.async { implicit request =>
  request.body.asJson.map(_.validate[ClientModel]) match {
    case c: JsSuccess[ClientModel] =>
      clientDTO.createClient(c.get).map{
        cnt => Created(Json.obj("id" -> cnt.id))
      }.recover {
        case e: Exception => BadRequest("Could not create client")
      }
    case err: JsError => Future.successful(BadRequest("TEST"))
  }
}

我认为你应该删除getOrElse部分。将它们添加到上面让我们看看。