简体   繁体   中英

Play! could not find implicit value for parameter reducer

I'm following along the 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.

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]

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

To validate multiple json fields you need to use 'pickBranch' which returns a JsObject (which is basically the equivalent of a Map[String, JsValue]). I'm guessing that reduce is a merge operation for several JsObjects.

I actually still haven't found a good use case for 'pick'. The '\\' syntax seems to do the equivalent job with less code and clutter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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