繁体   English   中英

在播放应用程序中调用模块控制器的REST API

[英]Call a REST API of a module controller in a play application

我正在编写一个Play 2.3.2应用程序。 我有用Java编写的主应用程序,还有用Scala编写的主应用程序使用的模块。

我的模块应用程序提供了一些REST API,这些API从JSON获取数据并返回JSON输出。

如何从我的主应用程序调用模块的REST API?

以及如何解析模块的Controller返回的计算结果?

@编辑

在我的模块控制器中,我有一些类似以下的方法:

object BridgeController extends Controller {
def addTagToUser = CorsAction.async { request =>
     //update the tag of a user
     def updateTagToUserDB(user: User, tag: Tag): Future[Boolean] = {
       println(tag)
       val query = Json.obj("id" -> user.id,
                            "email" -> user.email) //the query object
       Users.find(query).toList.flatMap { users =>
                        val oldUser = if (users.size > 0) users(0) else user
                        val newUser = oldUser.addTags(List(tag)) //create the updated user
                        //update the document find, and create if doesn't exists
                        Users.update(query, newUser, upsert = true).flatMap{error => error match {
                                    case LastError(ok, _, _, _, _,_, _) => Future{true}
                                    case _ => Future{false} //error on update the document
                        }}                  
       }
     }


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

     }
   }

}

在我的主应用程序的控制器中,我可以有以下内容:

public class Application extends Controller {
     public static Result addProduct(Product product) {
             User user = product.user()
             String category = product.category()
             String nameProduct = product.nameProduct()
             //here i want to create a json like {"user": {"id" : user.id, "email",         //user.email}, tag: {"category": category, "attr", nameProduct}}
            //now i want to sent an http request to the controller module and pass the json created



    }

}

如果要通过PLAY框架库使用REST服务,则可以使用WS API

https://www.playframework.com/documentation/2.3.3/ScalaWS

另一种选择是使用标准的Java EE

http://docs.oracle.com/javaee/7/tutorial/doc/jaxrs-client001.htm

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM