Scala + JDK 6 annotations = Simple REST service in ~ 50 LOC

This is a post I’d been meaning to submit for some time and is the chaser sibling to the SOAP fuelled scrub up, showing how to create a very simple service in the Restful style using only Java 6 and the Scala language (note I personally use Scala v2.9). Essentially this is a Scala port of otherwise published material describing REST support in Java 6, but I’d still found value in publishing the Scala version in the hope that it might help others trying to prototype services in the RESTful style without having to switch context and language or pull in a disparate set of libraries to support their efforts. Something to note, is that as this uses the minimal HTTP server support included in Java 6, certain features are unavailable (such as being able to interrogate the Request object for the clients IP etc.. as there is no servlet container included in Java 6 bundle).

To compile the example, simply save the code to a file (e.g. MinimalRestServer.scala) and compile with the standard scalac command. This will compile the classes into a folder called output. The server can then be started by issuing the command scala output.MinimalRestServer. Once the server has started the command line/shell should print the line: Waiting for requests.... You can then make a sample call in the order of: http://localhost:8084/helloWorld and get the expected output. Further testing can be done (using different HTTP methods) using the excellent cURL tool. Otherwise the code snippet below should be [reasonably] self explanatory.

Happy hacking and good luck !