本文整理汇总了Scala中play.api.http.Status.OK类的典型用法代码示例。如果您正苦于以下问题:Scala OK类的具体用法?Scala OK怎么用?Scala OK使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了OK类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: Tweet
//设置package包名称以及导入依赖的类
package io.digitalstream.webfx
import java.util.concurrent.atomic.AtomicLong
import javax.inject.{Inject, Singleton}
import akka.util.ByteString
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import play.api.http.Status.OK
import play.api.http.{ContentTypes, HttpEntity}
import play.api.mvc._
import scala.concurrent.ExecutionContext
case class Tweet(id: Long, author: String, content: String)
@Singleton
class TweetService @Inject()() {
val counter = new AtomicLong(1000000L)
def list(): List[Tweet] = {
List(
Tweet(counter.getAndIncrement(), "author1", "Hello, World!")
)
}
}
@Singleton
class TweetApi @Inject()(tweetService: TweetService)
(implicit ec: ExecutionContext)
extends Controller with JsonResults {
def list() = Action { req =>
JsonOk(tweetService.list())
}
}
trait JsonResults {
val mapper = new ObjectMapper()
.registerModule(DefaultScalaModule)
class JsonStatus(status: Int)
extends Result(header = ResponseHeader(status), body = HttpEntity.NoEntity) {
def apply[C](content: AnyRef): Result = {
val header = ResponseHeader(status)
val json = mapper.writeValueAsBytes(content)
val body = HttpEntity.Strict(ByteString(json), Some(ContentTypes.JSON))
Result(header, body)
}
}
val JsonOk = new JsonStatus(OK)
}