简体   繁体   中英

Play 2.1 for Http Server

I am new to play framework. My requirement is very simple. I want to create rest api server in scala using play framework. I could use play2-mini, but it seems it is outdated.

I want play 2.1 to be used in my project. Instead of setting play framework as dependency, I want only core module. So I have few questions -

  1. What is core module of Play ? what is module name ?
  2. Is it sufficient to use core module for creating asynchronous http server ?
  3. This link says I can use core module instead of play-mini. If it's true, where can I get more info about it.

You can just simply setup a route and then point it to a controller that parses the data you send. Here is an example of json parsing and serving back a response with play.

http://www.playframework.com/documentation/2.1.1/ScalaJsonRequests

package controllers

import play.api._
import play.api.mvc._
import play.api.libs.json._
// you need this import to have combinators
import play.api.libs.functional.syntax._

object Application extends Controller {

  implicit val rds = (
    (__ \ 'name).read[String] and
    (__ \ 'age).read[Long]
  ) tupled

  def sayHello = Action { request =>
    request.body.asJson.map { json =>
      json.validate[(String, Long)].map{ 
        case (name, age) => Ok("Hello " + name + ", you're "+age)
      }.recoverTotal{
        e => BadRequest("Detected error:"+ JsError.toFlatJson(e))
      }
    }.getOrElse {
      BadRequest("Expecting Json data")
    }
  }
}

or even simpler...

def sayHello = Action(parse.json) { request =>
    request.body.validate[(String, Long)].map{ 
      case (name, age) => Ok("Hello " + name + ", you're "+age)
    }.recoverTotal{
      e => BadRequest("Detected error:"+ JsError.toFlatJson(e))
    }
  }

Play Framework is a highly modular project. Internally it consists of around 20 subprojects. Some of them you can include in your project as a library dependency if you need them, for example anorm or jdbc . Other projects (ie PlayExceptions , RoutesCompiler , TemplatesCompiler etc.) are essential for any Play application, so you don't need to declare dependency on them. These projects could be called a 'core' of Play Framework.

In other words, if you need a Play application with minimum dependencies, just don't declare dependencies you don't need.

Play sources: https://github.com/playframework/Play20

I don't think there is much problem here. For example, this is my build.sbt used for a very small project, that uses json, in which I wanted to use Play libraries, but not necessarily create a full Play app:

name := "my-small-project"

version := "0.0.1-SNAPSHOT"

resolvers ++= Seq(
    "TypeSafe Repo" at "http://repo.typesafe.com/typesafe/releases",
    "Sonatype Repo" at "http://oss.sonatype.org/content/repositories/releases"
)

libraryDependencies ++= Seq(
    "org.specs2" %% "specs2" % "2.3.7" % "test",
    "commons-codec" % "commons-codec" % "1.8",
    "com.typesafe.play" % "play_2.10" % "2.2.1",
    "com.typesafe.play" % "play-json_2.10" % "2.2.1"
)

And You could still remove some dependencies here, especially if You don't need Base64 encoding. Here I consider play_2.10 as "the core" you're interested in. You should get yourself familiar with sbt though, but it's not that hard.

Also remember, that the difference between "a Play application" and "an application that uses Play libraries" is quite fuzzy, especially if you use sbt. And this is the beauty of it, this shows how Play creators thoughtfully tried not to invent a wheel once again by creating custom system requirements for their project to build. You can enter a Play app dir and type "sbt compile" for example instead of firing a Play console and it should work just fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM