如何在play-framework中使用json数据重定向到另一个Action

时间:2017-03-03 11:12:16

标签: java json scala playframework playframework-2.3

我有以下代码:

case class ResetPasswordJsonValidation (id : String ,email : String)

object ResetPasswordJsonValidation {

  implicit val resetPasswordRead : Reads[ResetPasswordJsonValidation]= (
    (JsPath \ "email").read(email) and
    (JsPath \ "id").read(id)
  )(ResetPasswordJsonValidation.apply _)

}

使用以下Action

def resetPassword = Action { request =>
    request.body.asJson.get.validate[ResetPasswordJsonValidation].fold(
        resetPassword => {
            log.info("id is {}", resetPassword.id)
            log.info("email id is {}", resetPassword.email)
        }
    )
}

路线档案:

POST    /direct-user/reset-password    controllers.DirectUserController.resetPassword

这是curl文件,我用它来触发上述路线的请求:

contentType="Content-type: application/json";

data='{  "id" : "54d3732d-d728-40d3-ae63-b18ab6be8e70" ,
       "email":"bob@example.com"}';
echo "    "
echo "------------------   Sending Data   ------------------"
echo "    "
echo "Content-Type : " $contentType
echo "Data : " $data


echo "    "
echo "------------------     Response     ------------------" 
echo "    "
echo "    "

curl命令:

curl --include --request POST --header "Content-type: application/json"  --data "$data" http://localhost:9000//direct-user/reset-password

我想要做的是让另一个动作重定向到resetPassword,但保留身体。怎么做?

1 个答案:

答案 0 :(得分:1)

使用闪光范围

def A = Action {
    var email:String="bob@example.com"
    var id:String="54d3732d-d728-40d3-ae63-b18ab6be8e70"
    Redirect(routes.DirectUserController.resetPassword()).flashing(
        "email" -> email,
        "id" -> id
    )  
  }

文档:https://www.playframework.com/documentation/2.5.x/ScalaSessionFlash

您需要编写特殊代码以从闪存范围获取变量,例如

request.flash.get("email")