Reputation: 2530
I am experimenting with aka and spray, what I want to achieve is a simple object marshalling service.
When I try to compile the code I get the following error :
Error:(33, 18) could not find implicit value for parameter marshaller: spray.httpx.marshalling.Marshaller[ExampleApplication.Password] marshal(Password(randomString(8),i,0)) ^
Here is the code:
import akka.actor.ActorSystem
import spray.http.HttpEntity
import spray.json.DefaultJsonProtocol
import spray.routing.SimpleRoutingApp
import spray.httpx.marshalling._
import spray.json._
object ExampleApplication extends App with SimpleRoutingApp {
implicit val actorSystem = ActorSystem()
implicit var i = 0;
object MyJsonProtocol extends DefaultJsonProtocol {
implicit val PasswordFormat = jsonFormat3(Password)
}
case class Password(pass: String, count: Int, complexity: Int)
def newPass(cplx: Int):Password = {return Password(randomString(cplx),i,0)}
startServer(interface = "localhost", port = 8080) {
get {
path("passgen") {
i+=1
complete {
marshal(newPass(8))
}
}
}
}
def randomString(n: Int): String = {
n match {
case 1 => util.Random.nextPrintableChar().toString
case _ => util.Random.nextPrintableChar.toString ++ randomString(n - 1).toString
}
}
}
I'm still failing to understand what's going wrong.
Upvotes: 1
Views: 559
Reputation: 4182
Two changes to fix it:
import spray.httpx.SprayJsonSupport._
Then even though you define the JsonProtocol object right in your app you must still import it's members explicitly:
object MyJsonProtocol extends DefaultJsonProtocol {
implicit val PasswordFormat = jsonFormat3(Password)
}
import MyJsonProtocol._
That looks a little repetitive in this case, but in most use cases you'll have it defined somewhere else.
Optional
You can complete the call without explicitly calling marshal
:
complete {
newPass(8)
}
Upvotes: 2