Your SlideShare is downloading. ×
Building Apis in Scala with Playframework2
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Building Apis in Scala with Playframework2


Published on

My talk at Silicon Valley Code Camp 2013. …

My talk at Silicon Valley Code Camp 2013.

Two years ago I gave a talk on PlayFramework with Java at SVCC. As with everything, PlayFramework has evolved into a far mature ecosystem with native Scala support, and Typesafe backing. PlayFramework 2 is a simple, MVC-based, convention over configuration web application framework for Java and Scala. It is rapidly gaining popularity as more and more companies are adopting it for building scalable, performant, share-nothing architectures. In this talk I'll cover the fundamentals of Play! Framework2, a brief overview of Scala, and demonstrate building a simple, RESTful API. This will be a very interactive, and hands-on session. It'd be awesome if you have played around with Scala, but if you have not, the scope of this talk does not require you to be a Scala expert. It will however be very useful if you've worked with other Web Application Frameworks like Jersey, CXF, etc. so you can cross-relate the concepts.

Published in: Technology, Education

1 Comment
1 Like
  • For those who did not attend this talk in person - the ArrayBuffer on Slide 27 is the 'simplest' example of where you would use a real database in the real world. Never, ever use mutable state in your models! The methods in the model would all talk to a storage layer or a storage system and not a mutable collection.
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 1. Building APIs in Scala with Play! Framework 2 Silicon Valley Code Camp Oct 5th, 2013 Manish Pandit @lobster1234
  • 2. Manish Pandit @lobster1234
  • 3. Agenda Scala Typesafe Platform PlayFramework REST Hands-on API Open Floor
  • 4. Scala Concise, yet expressive Statically Typed via Type Inference Rich Constructs Functions as First Class Citizens Power of DSL Interoperability with the Java Ecosystem
  • 5. Typesafe Platform Scala Programming Language PlayFramework for Web Applications sbt for builds Akka Typesafe Activator Misc. Tools
  • 6. Typesafe Activator A very simple way to start with Scala and Play! Applications Browser-based Interactive Comes with a good number of templates
  • 7. Typesafe Activator
  • 8. Typesafe Activator Use if you do not have an IDE Use it to play with various templates it comes with Yet to mature but a (very) good start
  • 9. Play! Framework Simple Convention over Configuration Focus on Developer Productivity Asynchronous Processing Model Makes testing easy JSON support for RESTFul Applications
  • 10. Play! Framework Not a J2EE compatible framework Does not follow the traditional request/response model
  • 11. Creating a Play! Project
  • 12. Using an IDE
  • 13. Eclipse Import
  • 14. Gotchas! Start with a fresh installation of ScalaIDE Go to Install New Software – Select ScalaIDE – Check Play2 Plugin
  • 15. Run the App!
  • 16. And Play! with it http://localhost:9000 Test the app with play test
  • 17. Play! Core Concepts Uses sbt as the build system (In a special way to support always-on builds)
  • 18. Play! Core Concepts MVC Folder Structure Controllers Routes File Tests
  • 19. Play! Actions In simple terms, an Action takes function, that takes a request Request, and returns a Response.
  • 20. Play! Actions All routable methods of a Controller are Actions (Request => Result) function
  • 21. The almighty Action A simple, no-request that returns a result. A function that makes the Request available to the block You can pass a BodyParser to the Action as an argument Default BodyParsers are available for common content types
  • 22. Play! Console Demo run vs. ~run With ~ run or run, change the app with syntax error Fix the app and retry Use the app with ~ test in Play! Console Change the code and watch the tests fail Change the tests and watch them pass
  • 23. The Routes file Type-safe way to define mappings from URLs to Actions Rich support for basic data types Handling of defaults
  • 24. RESTFul APIs Not JSON over HTTP Not JSON representation of your Database No Spec or official standard
  • 25. RESTFul APIs Envision the entities as HTTP resources Envision the interactions as HTTP methods Read RFC 2616
  • 26. Play! Hands-on API Model – A Person APIs – Get all, Get by First/Last/Zip, Add a person JSON Request and Response
  • 27. Model object PersonModel { val persons = ArrayBuffer(Person("Manish", "Pandit", 94568), Person("John", "Doe", 95051), Person("My", "Neighbor", 94568)) def add(p: Person) = persons += p def getAll = persons def getByFirstName(first: String) = persons.filter(_.first == first) def getByLastName(last: String) = persons.filter(_.last == last) def getByZip(zip: Int) = persons.filter( == zip) } case class Person(first: String, last: String, zip: Int)
  • 28. Controller object SimpleAPI extends Controller { implicit val personWrites = Json.writes[Person] implicit val personReads = Json.reads[Person] def getAll = Action { Ok(Json.toJson(PersonModel.getAll)) } def add = Action(parse.json) { request =>"Body is $request.body") request.body.validate(personReads) match { case x: JsError => BadRequest case x: JsSuccess[Person] => PersonModel.add(x.get) Created } } def getByFirstName(first: String) = Action { val list = PersonModel.getByFirstName(first) if (list.isEmpty) NotFound else Ok(Json.toJson(PersonModel.getByFirstName(first))) } }
  • 29. Routes # ~~~~ # Routes # This file defines all application routes (Higher priority routes first) # ~~~~ # Home page GET /persons controllers.SimpleAPI.getAll GET /persons/first/:first controllers.SimpleAPI.getByFirstName(first) GET /persons/last/:last controllers.SimpleAPI.getByLastName(last) GET /persons/zip/:zip controllers.SimpleAPI.getbyZip(zip:Int) GET /persons/default controllers.SimpleAPI.getByFirstName(first="Manish") POST /persons controllers.SimpleAPI.add
  • 30. Play! Packaging Use play dist to package the app as a zip file Run using the start script
  • 31. Play! Advanced Topics Reactive Programming Akka, Futures, Promises Play WS Library Anorm and Slick
  • 32. Further reading StackOverflow Source :
  • 33. We are hiring!
  • 34. Fin Manish Pandit @lobster1234