silicon valley code camp 2011: play! as you rest
Post on 19-Oct-2014
2.508 views
DESCRIPTION
My talk from Silicon Valley Code Camp 2011 on PlayFramework.TRANSCRIPT
Play! as you REST
Using Play! Framework to build RESTful Services
Silicon Valley Code CampOctober 8th, 2011
Agenda
• RESTful Interface• Traditional J2EE Development• Rise of developer-friendly and fun frameworks• Play! Framework Overview• Working Session – Implement and Deploy a
RESTful service using Play! Framework (Java).
What is REST
• It supports HTTP as the Transport Protocol• The HTTP verbs map to the action being performed
on a resource• The resource is the noun on which an action is being
performed. The noun is generally qualified by an identifier.
• The noun is a business entity, a part of the model.• REST is not a standard, but an architecture. Hence
everyone tends to explain it differently but drives to the same meaning.
Examples of RESTful URLs• GET /v1/person/1234
• POST /v1/person/1234/status{“status”: “Hello, developers!”}
• DELETE /elasticsearch/people/status/_mapping
• PUT /v2.0/photos/e5c2f436-f149-11e
• GET /v1.0/social/rest/people/nickname.mpanditign/@friends
• GET https://accounts.google.com/o/oauth2/token
Traditional J2EE Web Development
1. Code a servlet (or something that becomes a servlet down the line) using Jersey or Spring, etc.
2. Stick the name and path in a web.xml3. Package a war file via a build process like ant,
maven, ivy, etc.4. Copy the war file in the container’s webapp folder5. Bounce the server6. Pray that it works, if not, go to step 1.
Rise of Developer-friendly Frameworks
• Faster turnaround and prototyping• No need for build -> deploy -> fix -> build cycle• Integrated Testing Support via Specs• Convention over Configuration• Lightweight, focused towards making rapid
development and deployment a priority. • Developers can focus on the fun stuff, instead
of writing configurations and build scripts
Play! Framework
• Inspired by Django and Rails• A complete web-framework offering:
– A Server (netty)– IDE integration with Eclipse, TextMate, IntelliJ– Templating Language (Groovy, Scala [2.0])– RESTful by being on a share-nothing, stateless architecture– Modules for all major dependencies and seamless integration– Hot Reload - you need the IDE and the Browser. No build scripts.– Convention Over Configuration– None of the J2EE bloat
Play! Overview
Play! Components
• The Controller – Which gets requests, and has the logic to process them
and generate a response by interacting with the Model• The routes file– Where URLs are mapped to Controller Methods
• The View– Templates, etc. to render the response from the
controller• The Model– The “beans” or data-aware domain objects.
Demo Time!
• Download Play! 1.2.3 • Build a simple Play! Java Application• Go over the folder structure• eclipsify it, pull in Eclipse• Demonstrate the hot-reload– Add a new method in the controller
• Run tests via the built-in TestRunner– Mess up a test– Add a new test for the new method
More Goodies hidden in Play!
• Quick persistence without a database?– db=mem
• Memcached support• MySQL and PostgreSQL support• Test Data stored as Fixtures in a YAML• Modes – dev, production
– dev shows detailed errors with source code• Mail hosts• Scheduled Jobs via annotations (demo later?)• Built in Logger, so you wont work with awesome log4j
configs.
Bundled Libraries
• Play comes bundled with– ehcache– hibernate ORM– apache commons– Google Gson– ezmorph– MySQL and Postgres drivers– memcached driver– xstream for XML– Antlr– …and more!
Let’s build an API
• A system to store and query users– Create a (very) simple model – Use MongoDB to persist the model in the db
Service Endpoints
• GET all users /users• GET a user by ID /users/{id}• DELETE a user by ID /users/{id}• POST (create) a new user /users• PUT (update) a user /users/{id}
Things I am not covering
• Custom deserialization of Mongo ObjectId() with Gson
• Merges on Updates• Caching using built-in Cache façade
What to look forward to
• Play! 2.0– Scala as the templating language– Support for sbt as the build system (vs. Python)– Stateless JPA called Ebean instead of Hibernate– Native support for both Java and Scala
We’re hiring!corp.ign.com/careers, and @ignjobs
• Scala• Java• PHP/Zend• Rails• ElasticSearch• MongoDB• MySQL• HTML5
• Jquery Mobile• Sencha Touch• Phonegap• Wordpress• ActionScript/Flash• Redis/Memcached• CI/CD
About
Manish Pandit Sr. Engineering ManagerIGN Entertainment
http://linkedin.com/in/mpandit@lobster1234