问题
I'm pretty new to Play! and scala and i'm searching desperatly for a good step by step tutorial on how to implement a webservice. the documentation is pretty poor and i can't find something that helps.
ps: i have already done the exemple given in the playframework web site it helped a lot for the understanding of the framework but my knowledge to scala is the big obstacle here.
回答1:
Well i think this is what i wanted. First lets assume that we want a RESTfull webservice that returns informations about a user. we create the user class as following
case class User() {
val id= 1
val name = "john"
val score = 8.5
}
then we make the controller which is as follow
object Application extends Controller {
def sum() = Action {
val user = new User
val json = Json.generate(user)
Ok(json).as("application/json")
}
}
and don't forget to add the import for Json which is import com.codahale.jerkson.Json
For the route add the following line to your route
file:
GET /sum controllers.Application.sum
the result should look something like
{
"id":1,
"name":"john",
"score":8.5
}
来源:https://stackoverflow.com/questions/12180475/play-framework-webservice-tutorial-scala