Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play Framework webservice tutorial scala [closed]

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.

like image 940
Marouane Lakhal Avatar asked Aug 29 '12 14:08

Marouane Lakhal


1 Answers

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
}
like image 160
Marouane Lakhal Avatar answered Sep 20 '22 14:09

Marouane Lakhal