Play Framework webservice tutorial scala [closed]

自作多情 提交于 2020-01-24 04:12:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!