[英]how to get the response of called Action within an Action in play framework
i have two Actions in different controllers ActionA
and ActionB i am calling ActionB
in ActionA and i want to get its(ActionB) response in ActionA is it possible ? 我在不同的控制器
ActionA
和ActionB中有两个Action,我在ActionB
中调用ActionB,我想在ActionA中获得它的(ActionB)响应吗? how can i achive this please help here is my code 我怎么能做到这一点,请帮助我的代码
class ControllerA extends Controller{
def ActionA = Action { implicit request =>
var jsonRequest = request.body.asJson.get
val uuid = (jsonRequest \ "uuid").as[String]
log.info("in ActionA" + uuid)
val controllerB= new ControllerB
val actionB=controllerB.ActionB.apply(request)
//here i want to get the response of ActionB and return this response as the response of ActionA whether its OK or InternelServerError
Ok("i want to show the response of ActionB")
}
}
class ControllerB extends Controller{
def ActionB = Action { implicit request =>
var jsonRequest = request.body.asJson.get
val uuid = (jsonRequest \ "uuid").as[String]
log.info("in ActionB " + uuid)
try {
Ok("i am ActionB with id {}"+uuid)
} catch {
case e: Exception =>
log.error("Exception ", e)
val status = Http.Status.INTERNAL_SERVER_ERROR
InternalServerError(Json.obj("status" -> status, "msg" -> ServerResponseMessages.INTERNAL_SERVER_ERROR))
}
}
}
please help 请帮忙
In play 2.2 and 2.3 controllers are typically an object
instead of a class
so I changed your controllers to be objects. 在播放2.2和2.3中,控制器通常是
object
而不是class
因此我将您的控制器更改为对象。 In newer versions of play controllers are classes that are injected using the Guice framework. 在较新版本的播放控制器中,是使用Guice框架注入的类。
Since invocations of actions is asynchronous, you need to change ActionA
to be Action.async
. 由于动作的调用是异步的,因此您需要将
ActionA
更改为Action.async
。 Below are the changes that I made: 以下是我所做的更改:
object ControllerA extends Controller{
def ActionA = Action.async { implicit request =>
var jsonRequest = request.body.asJson.get
val uuid = (jsonRequest \ "uuid").as[String]
log.info("in ActionA" + uuid)
ControllerB.ActionB(request)
}
}
object ControllerB extends Controller{
def ActionB = Action { implicit request =>
var jsonRequest = request.body.asJson.get
val uuid = (jsonRequest \ "uuid").as[String]
log.info("in ActionB " + uuid)
try {
Ok("i am ActionB with id {}"+uuid)
} catch {
case e: Exception =>
log.error("Exception ", e)
val status = Http.Status.INTERNAL_SERVER_ERROR
InternalServerError(Json.obj("status" -> status, "msg" -> ServerResponseMessages.INTERNAL_SERVER_ERROR))
}
}
}
As the previous answer alluded to, it's far more advantageous to have shared controller code in a service layer that sits below your controllers as opposed to sharing controller code directly. 正如前面的答案所暗示的,与直接共享控制器代码相比,在位于控制器下方的服务层中共享控制器代码更为有利。 Given your simplistic example though it seems OK to do what you're doing.
考虑到您的简单化示例,可以执行您正在做的事情。
If you deploy your controllers in a single JVM, I think you can extract a function from ActionB and share the code between two controllers. 如果您将控制器部署在单个JVM中,我认为您可以从ActionB中提取一个函数并在两个控制器之间共享代码。 If you deploy your controllers in two different JVMs, in this case you need to use a web service client library to query the endpoint.
如果将控制器部署在两个不同的JVM中,则在这种情况下,您需要使用Web服务客户端库来查询端点。 Just my two cents.
只是我的两分钱。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.