Silicon Valley Code Camp 2011: Play! as you REST

18
Play! as you REST Using Play! Framework to build RESTful Services Silicon Valley Code Camp October 8 th , 2011
  • date post

    19-Oct-2014
  • Category

    Technology

  • view

    2.506
  • download

    1

description

My talk from Silicon Valley Code Camp 2011 on PlayFramework.

Transcript of Silicon Valley Code Camp 2011: Play! as you REST

Page 1: Silicon Valley Code Camp 2011: Play! as you REST

Play! as you REST

Using Play! Framework to build RESTful Services

Silicon Valley Code CampOctober 8th, 2011

Page 2: Silicon Valley Code Camp 2011: Play! as you REST

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).

Page 3: Silicon Valley Code Camp 2011: Play! as you REST

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.

Page 4: Silicon Valley Code Camp 2011: Play! as you REST

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

Page 5: Silicon Valley Code Camp 2011: Play! as you REST

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.

Page 6: Silicon Valley Code Camp 2011: Play! as you REST

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

Page 7: Silicon Valley Code Camp 2011: Play! as you REST

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

Page 8: Silicon Valley Code Camp 2011: Play! as you REST

Play! Overview

Page 9: Silicon Valley Code Camp 2011: Play! as you REST

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.

Page 10: Silicon Valley Code Camp 2011: Play! as you REST

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

Page 11: Silicon Valley Code Camp 2011: Play! as you REST

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.

Page 12: Silicon Valley Code Camp 2011: Play! as you REST

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!

Page 13: Silicon Valley Code Camp 2011: Play! as you REST

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

Page 14: Silicon Valley Code Camp 2011: Play! as you REST

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}

Page 15: Silicon Valley Code Camp 2011: Play! as you REST

Things I am not covering

• Custom deserialization of Mongo ObjectId() with Gson

• Merges on Updates• Caching using built-in Cache façade

Page 16: Silicon Valley Code Camp 2011: Play! as you REST

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

Page 17: Silicon Valley Code Camp 2011: Play! as you REST

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

Page 18: Silicon Valley Code Camp 2011: Play! as you REST

About

Manish Pandit Sr. Engineering ManagerIGN Entertainment

http://linkedin.com/in/mpandit@lobster1234