1

다른 컨트롤러에 두 개의 액션이 있습니다 ActionA 및 ActionB ActionA에서 ActionB을 호출 중이며 ActionA에서 ActionB 응답을 받고 싶습니다. 내가이 여기에 도움을 주시기 바랍니다 achive 수있는 방법을하는 단일 JVM에서 컨트롤러를 배포하는 경우재생 프레임 워크의 작업 내에서 호출 된 작업의 응답을 얻는 방법

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)) 
    } 
    } 
} 

답변

1

게임에서 2.2 및 2.3 컨트롤러는 class 대신 일반적으로 object이므로 컨트롤러를 개체로 변경했습니다. 최신 버전의 Play 컨트롤러에는 Guice 프레임 워크를 사용하여 주입되는 클래스가 있습니다.

작업의 호출이 비동기이기 때문에 ActionAAction.async으로 변경해야합니다. 아래는 내가 변경 한 내용은 다음과 같습니다

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)) 
    } 
    } 
} 

이전 답변에 언급, 그것은 직접 컨트롤러 코드를 공유 반대로 당신의 컨트롤러 아래에 앉아 서비스 계층에서 공유 컨트롤러 코드가 훨씬 더 유리합니다. 당신이하는 일을하는 것이 괜찮은 것 같지만 당신의 단순한 예를 생각해보십시오.

+0

감사합니다. – swaheed

0

도와주세요 내 코드를, 당신이 ActionB에서 함수를 추출하고 두 컨트롤러 사이의 코드를 공유 할 수 있다고 생각 . 두 개의 서로 다른 JVM에 컨트롤러를 배포하는 경우이 경우 웹 서비스 클라이언트 라이브러리를 사용하여 끝점을 쿼리해야합니다. 그냥 내 두 센트.

+0

예 단일 JVM에서 ActionA의 ActionB 응답을받는 방법 코드 예제 – swaheed