向我的play应用程序的另一个方法发送json值

向我的play应用程序的另一个方法发送json值,json,scala,http,playframework,Json,Scala,Http,Playframework,我正在用Scala编写Play 2.3.2应用程序。 在我的应用程序中,我有application对象,在该对象中,我有advice方法,该方法以Json格式获取一些数据并返回Json响应 现在我正在编写另一个对象,它获取Json中的一些数据,并调用传递Json数据的advice方法,我如何在游戏中做到这一点 我在网上搜索过,但什么也没找到 @编辑 我的项目是另一个Java play应用程序使用的模块 问题是该方法返回一个json,但在某些情况下java应用程序不需要json响应,但Ok响应就足

我正在用Scala编写Play 2.3.2应用程序。 在我的应用程序中,我有
application
对象,在该对象中,我有
advice
方法,该方法以Json格式获取一些数据并返回Json响应

现在我正在编写另一个对象,它获取Json中的一些数据,并调用传递Json数据的
advice
方法,我如何在游戏中做到这一点

我在网上搜索过,但什么也没找到

@编辑

我的项目是另一个Java play应用程序使用的模块

问题是该方法返回一个json,但在某些情况下java应用程序不需要json响应,但Ok响应就足够了

因此,我正在编写的方法具有以下原型:

   def addTagToUser = CorsAction.async { request =>
       def elaborate(json: JsValue): Future[Result] ={
           //here I want to send an http request to the advise method and send the json to that.
       }

 val jsonData = request.body.asJson //get the json data
     jsonData match {
       case Some(x) => val user = x \ "user"; val tag = x \ "tag";
                    (user \ "id", user \ "email", tag \ "category", tag \ "attr") match {
                      case (userId: JsString, userEmail: JsString,
                          tagCat: JsString, tagAtr: JsString) => val myUser = new User(userId.as[String], Some(userEmail.as[String]))
                                                                 val newTag = new Tag(tagCat.as[String], tagAtr.as[String])
                                                                 updateTagToUserDB(myUser, newTag).flatMap(status => status match {
                                                                   case true => Future{Ok}//the update was executed correctly
                                                                   case false => Future{InternalServerError("Cannot access to the db now")}//update fail
                                                                 }
                                                                   )
                      case _ => Future{BadRequest("json bad formed")} // the json is bad formed

                    }

       case None => Future{BadRequest("need a json value")}

     }
   }

如果您完全希望通过HTTP进行通信,则需要使用该剧的Web服务来实现。请查看WS:


如果你有一个从一个项目到另一个项目的显式依赖关系,那么只提取转换json的方法,然后用它构建一个Ok,可能会更简单…

创建一个“内部”方法,并在你喜欢的任何地方以任何方式调用它;除非在两个不同的服务器/服务之间调用,否则不要使用JSON进行内部通信。然后,您可以从代码中调用该方法并获取静态类型的返回值,或者使用包装代码将该方法映射到URL,包装代码在JSON和内部数据类型之间封送

如果您提供了一些代码,或者至少提供了方法的签名,那么回答您的问题就会更容易。顺便说一句,您的json是原始字符串还是已经是play api中的json对象了?所以我想,
advice
是一个
操作。如果是这样的话,只需从advice中提取语句和调用,并将它们放在一个特定的方法中,您可以在任何地方调用它们。我已经在我的问题上发布了我的代码,请参阅我的@edit
   def addTagToUser = CorsAction.async { request =>
       def elaborate(json: JsValue): Future[Result] ={
           //here I want to send an http request to the advise method and send the json to that.
       }

 val jsonData = request.body.asJson //get the json data
     jsonData match {
       case Some(x) => val user = x \ "user"; val tag = x \ "tag";
                    (user \ "id", user \ "email", tag \ "category", tag \ "attr") match {
                      case (userId: JsString, userEmail: JsString,
                          tagCat: JsString, tagAtr: JsString) => val myUser = new User(userId.as[String], Some(userEmail.as[String]))
                                                                 val newTag = new Tag(tagCat.as[String], tagAtr.as[String])
                                                                 updateTagToUserDB(myUser, newTag).flatMap(status => status match {
                                                                   case true => Future{Ok}//the update was executed correctly
                                                                   case false => Future{InternalServerError("Cannot access to the db now")}//update fail
                                                                 }
                                                                   )
                      case _ => Future{BadRequest("json bad formed")} // the json is bad formed

                    }

       case None => Future{BadRequest("need a json value")}

     }
   }