I've made a pretty simple rest-method using Finch and Finagle:
val getUsers:Endpoint[List[User]] = get("users") {Ok(getAllUsers())}
Http.serve(":8080", getUsers.toService)
and got this error:
Error:(50, 32) not enough arguments for method toService: (implicit ts: io.finch.internal.ToService[List[DAL.Instances.User.User]])com.twitter.finagle.Service[com.twitter.finagle.http.Request,com.twitter.finagle.http.Response].
Unspecified value parameter ts.
Http.serve(":8080", getUsers.toService)
^
Any idea on how to fix it?
The compiler error is a little better in the most recent version of Finch (0.10). If we have the following build config:
And this setup:
Then when we try to use
toService
, we get the following:The problem is that you haven't told Finch how to translate instances of your
User
type into HTTP responses. Finch'sEncodeResponse
is an example of a type class, which is an approach to polymorphism that's widely used in Scala (including the standard library) and many other statically-typed functional programming languages.The easiest way to provide the appropriate
EncodeResponse
instances is to add Finch's Circe compatibility module to your build:And then all you need is the following imports:
And
toService
will work just fine:Now if you go to
http://localhost:8080/users
, you'll see the following:This looks like magic, but what's happening is fairly principled. Circe is a JSON library that provides generic codec derivation that figures out at compile-time how to represent your case classes as JSON values (see my blog post here for more context).
The Finch cookbook is a great resource for learning more about questions like this, and the first section goes into detail about other ways to provide the
EncoderResponse
instances thattoService
needs. If you have any other questions or if any of the above isn't clear, feel free to ask either here or on Gitter.