Simple JDBC Queries in Scala

The Beginning Scala book has a great example of using partially applied functions to automatically close JDBC connections. Today I needed to use some complex SQL outside of our ORM and extended this code sample to make it incredibly simple & safe. The using and bmap methods are from the book; the query and queryEach…

URI Extensions in Jersey

Jersey provides an excellent system for easily supporting multiple representations of resources. You use the @Produces annotation to define which formats a resource method supports: In this case clients can obtain the representation of the thing resource in either XML or JSON format (assuming you’ve registered corresponding MessageBodyWriters for the Thing class).  Just by including…

Same Snippet, Different Designs

You’re building a web site and the designers send you XHTML templates with essentially the same form on two different pages, but each page uses a slightly different layout. Being the good coder you are, you want to reuse the same submit-handling code on the back-end to prevent code duplication. This is easy to handle…

Using Dummy Data in Lift Templates

One of the awesome features of “view first” web frameworks like Lift and Wicket is that the page templates are stand-alone documents that are viewable without being served by the framework and don’t contain any server-side code.  This makes things much easier for web designers: they can create an XHTML template file using whatever tool…

XHTML Resource Representations in Jersey using StringTemplate

Most web services these days use XML and/or JSON as representation formats, mainly because they are simple to create and are easily parseable by other apps.  A less commonly used, but extremely useful format is XHTML.  But wait, that’s for displaying web pages right?  It sure is, but it’s also valid XML.  So if you…

Use Exceptions to Send Error Responses in Jersey

Jersey provides several different ways to send back particular HTTP responses.  While your resource methods can all return Response objects, sometimes it’s more natural to return a domain object if everything goes OK and throw exceptions when errors occur. The WebApplicationException class serves this purpose.  You throw an instance of it in your resource method,…

Override the HTTP Request Method in Jersey

RESTful web services take advantage of all of the HTTP methods including GET, POST, PUT, DELETE, HEAD and OPTIONS.  However, a lot of HTTP libraries (BlackBerry JDE, iPhone SDK, etc) and all browsers only support GET and POST.  There are several ways to get around these deficiencies – in this post I’ll describe two of…