/play2-mini

Play 2.0 mini project - provides REST API on top of Play2

Primary LanguageScala

Play2 Mini Project

Play2 Mini Project provides REST API on top of Play2

Scala

In the global package name space:

object Global extends com.typesafe.play.mini.Setup(com.example.App)

and then in your own package:

object App extends com.typesafe.play.mini.Application {
   def route  =  {
      case GET(Path("/coco")) & QueryString(qs) =>  Action{
          val o = QueryString(qs,"foo").getOrElse("noh")
          Ok(<h1>It works!, query String {o}</h1>).as("text/html")
      }
      case GET(Path("/flowers")) =>  Action{
          Ok(<h1>It works for flowers!</h1>).as("text/html")
      }
    }
}

The API is based on the always awesome Unfiltered library.

Extending the Extractor based approach with regex or simple matching is possible too:

object App extends com.typesafe.play.mini.Application {
  def route = Routes(
    Through("/people/(.*)".r) {groups: List[String] =>
      Action{
        val id :: Nil = groups
        Ok(<h1>It works with regex!, id: {id}</h1>).as("text/html")
      }
    },
    {case GET(Path("/coco")) & QueryString(qs) => Action{ request =>
        println(request.body)
        println(play.api.Play.current)
        val result = QueryString(qs,"foo").getOrElse("noh")
        Ok(<h1>It works!, query String {result}</h1>).as("text/html") }
    },
      Through("/flowers/id/") {groups: List[String] =>
        Action{
          val id :: Nil = groups
          Ok(<h1>It works with simple startsWith! -  id: {id}</h1>).as("text/html")
        }
      },
      Through("/cars/id/") {groups: List[String] =>
        ActorAction[String](myActor, Car) {reply: String =>
          Ok(reply)
        }
      }
  )
}

for more information about extractors, regex and Routes, please see

Other than this, there are many useful utilities available at your fingertip, take a look at the official guide of Play for Scala Developers

Java

In the global package name space:

//Global.scala
object Global extends com.typesafe.play.mini.SetupJavaApplicationFor[com.example.App]

and then in your own package:

 //com/example/App.java
 package com.example;

 public class App extends Controller {
   @URL("/hello")
   public static Result index() {
       return ok("It works as text!");
   }

  @URL("/foo/*/name/*")
   public static Result index2(String foo, String name) {
       response().setContentType("/text/html");
       return ok("It works:"+ foo +" "+name);
  }
}

Other than this, there are many useful utilities available at your fingertip, take a look at Play Java Utils and Async processing with Play

See sample apps for more examples.

G8 Project templates

G8 project templates are available as well.

java:

g8 pk11/play-mini-java.g8

scala:

g8 pk11/play-mini-scala.g8

Licence

The code is licensed under Apache 2 license:

This software is licensed under the Apache 2 license, quoted below.

Copyright 2011 Typesafe (http://www.typesafe.com).

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this project except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.