Friday, November 02, 2012

Austin Java Users Group - RESTful Web Services

On October 30, 2012 Ryan Stewart of Rackspace gave a wonderful presentation to fifty people on creating RESTful web service using Gradle for building, JAX-RS(Jersey) for the web services and Spock for testing. It was very impressive. In 100 minutes Ryan wrote the tests for a small web service in Groovy using the Spock framework, created the code to implement those tests, and deployed to Red Hat's cloud. He reiterated the mantra of HTTP verbs:
POST is to create
PUT is for updates
DELETE is for well, deleting
Ryan used the sublime editor for small things and IntelliJ for coding (he really liked IntelliJ more than Eclipse, even the community edition).
jacoco used for a code coverage tool.
"Test your source code with a more powerful language." So we used Groovy for testing.
I learned that the 201 status code is for "OK, I created an object" and by convention the objects location (identity) is passed back in a header.
Ryan liked, what .Net people call "Fluent" style, where methods can be chained together if we return the object, e.g., user.withRole("admin").expires("never")...
public User withRole(String role) {
  return this;
Ryan demonstrated "JerseyTest" which was amazing.
For REST systems, instead of wsdl like SOAP used, we have a simpler format called "wadl", which is not standard at the moment.
When retrieving info from a REST server we can imply the format by appending an extension, /usr/345.xml or /usr/345.json
JerseyTest impressed me with the excellent separation of code possible. Ryan went out of his way to write all the code to be testable, and only use the meta info on the methods to describe their web behavior, so we could test their POJO behavior easily with NUnit.
He used to autogenerate accessors.
"gradle distZip" created the distribution package
The most impressive part of the demo was to take our shiny new web service and seemlessly deploy to RedHat's OpenShift cloud service. His slides are here and the web service is here.

No comments: