简体   繁体   English

玩! 找不到参数reducer的隐式值

[英]Play! could not find implicit value for parameter reducer

I'm following along the Play! 我正在跟随Play! 2.1 coast-to-coast tutorial at http://mandubian.com/2013/01/13/JSON-Coast-to-Coast/ but cannot get even the most trivial example working. 2.1从海岸到海岸的教程, 网址http://mandubian.com/2013/01/13/JSON-Coast-to-Coast/,但即使最琐碎的示例也无法正常工作。

When I compile my project I get an error: 编译项目时出现错误:

could not find implicit value for parameter reducer: play.api.libs.functional.Reducer[play.api.libs.json.JsString,B] 找不到参数化简器的隐式值:play.api.libs.functional.Reducer [play.api.libs.json.JsString,B]

My controller code is as follows: 我的控制器代码如下:

package controllers

import play.api._
import play.api.mvc._

import play.api.libs.json._
import play.api.libs.json.Reads._
import play.api.libs.functional.syntax._

object MyController extends Controller{

  val validate = (
    (__ \ 'title).json.pick[JsString] and
    (__ \ 'desc).json.pick[JsString]
  ).reduce

  def test() = Action { implicit request =>
    Ok("test")
  }
}

What am I missing to get this working? 我缺少什么才能使它正常工作?

The syntax here is not quite right. 这里的语法不太正确。 'pick' returns a JsValue (the Play! equivalent of valid Json types including String, Array, etc). “ pick”返回一个JsValue(Play!等效于有效的Json类型,包括String,Array等)。

To validate multiple json fields you need to use 'pickBranch' which returns a JsObject (which is basically the equivalent of a Map[String, JsValue]). 要验证多个json字段,您需要使用“ pickBranch”,该返回一个JsObject(基本上等效于Map [String,JsValue])。 I'm guessing that reduce is a merge operation for several JsObjects. 我猜到reduce是几个JsObjects的合并操作。

I actually still haven't found a good use case for 'pick'. 实际上,我仍然没有找到'pick'的好用例。 The '\\' syntax seems to do the equivalent job with less code and clutter. '\\'语法似乎以更少的代码和混乱完成了等效的工作。

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

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