java - 在 Play 应用程序中调用模块 Controller 的 REST API

标签 java json scala rest playframework

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

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

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

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

@编辑

在我的模块 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")}

     }
   }

}

在我的主应用程序的 Controller 中,我可以有这样的东西:

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

关于java - 在 Play 应用程序中调用模块 Controller 的 REST API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26364475/

相关文章:

java - 进行 docusign api 握手时获取 javax.net.ssl.SSLHandshakeException

Scala 将两个整数相除并得到浮点结果

java - 如何在 Java 中使用 Flinks Splitter 方法

java - HttpGet/客户端和 HTTPS

java - 乔达 - 打印日期时间忽略时区

python - 在 python 中从 JSON 文件创建对象(使用类方法)?

javascript - Web Components,将数据传入和传出

scala - 无法解析符号 "Scalatest"

java - 在 EJB mdb 中初始化 Hibernate SessionFactory?

java - 链接两个作业时 hadoop.mapreduce.lib.input.FileInputFormat.getBlockIndex 中的 NullPointerException