Play Framework网络服务教程Scala [英] Play Framework webservice tutorial scala

查看:59
本文介绍了Play Framework网络服务教程Scala的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手!和scala,我正拼命寻找有关如何实现Web服务的逐步教程.该文档非常糟糕,我找不到有帮助的东西.

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:我已经完成了playframework网站上给出的示例,这对理解框架很有帮助,但是我对scala的了解是这里的最大障碍.

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.

推荐答案

好吧,我认为这就是我想要的.首先让我们假设我们想要一个RESTfull Web服务,该服务返回有关用户的信息.我们按照以下方式创建用户类

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")
  }
}

别忘了为Json添加导入文件,该导入文件为import com.codahale.jerkson.Json

and don't forget to add the import for Json which is import com.codahale.jerkson.Json

对于路由,将以下行添加到您的route文件中:

For the route add the following line to your route file:

GET     /sum                 controllers.Application.sum

结果应类似于

{
 "id":1,
 "name":"john",
 "score":8.5
}

这篇关于Play Framework网络服务教程Scala的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆