0

我正在尝试编写一个异步 PlayFramework 控制器来接收 POST 请求并在数据库中创建一个新对象:

  def register = Action(BodyParsers.parse.json) { request =>
    val businessInfoResult = request.body.validate[BusinessInfo]
    businessInfoResult.fold(errors =>{
      BadRequest(Json.obj("status"-> "Error", "message"->JsError.toJson(errors))) //Error on this line
    }, businessInfo=> {
      //save the object
      Ok(Json.obj("status" ->"OK", "message" -> ("Place '"+ businessInfo.businessName +"' saved.") )) //Error on this line

    })
  }

但是,它不断抛出以下错误:

reference to Json is ambiguous; it is imported twice in the same scope by import play.libs.Json and import play.mvc.BodyParser.Json AsyncController.scala   

错误在第 108 行和第 105 行抛出,它们对应于//Error on this line上面注释的行(带有 BadRequest(..) 和 Ok(..) 的行)

我该如何解决这个问题?我可以使用 new JsValue(Map(..)) 但想知道是否还有其他方法。

非常感谢你的帮助。

4

2 回答 2

0

您可以在导入中使用一个或多个别名:

import play.libs.Json
import play.mvc.BodyParser.{Json => JsonParser}

JsonParser只是一个例子。你可以使用任何你喜欢的东西,只要它在文件中是唯一的。

您现在可以使用别名,而不是编写Json(for ) 。play.mvc.BodyParser.JsonJsonParser

但是你确定你甚至需要 importplay.mvc.BodyParser.Json吗?因为你似乎没有使用它。

于 2016-04-25T12:56:28.920 回答
0

而不是Json,你可能想打电话play.libs.Json。这里的问题是,考虑到文件中的导入,您调用Json了两个对象/类,编译器无法选择应该使用哪一个。调用play.libs.Json,您将为编译器提供足够的信息。

于 2016-04-25T07:49:47.550 回答