Ribe Software’s blog

November 29, 2007

Making double clickable Java EE web applications

A few days ago I was playing with JAX-RS and Jersey when I thought “oh, what easy it is to run this kind of web applications” because of you don’t need a servlet container. Wait! I can run this applications using a plain main() method so… these kinds of application are desktop applications? I can make a main() method that launches the Http server and opens the web application in the default web browser. Mmmm and I can put a tray-icon to re-open web browser if the user closes it or to stop the web server. On balance your main() method does these things:

  • Launches the Http server that comes with Java SE 6
  • Opens the default web browser to any URL inside your application (http://localhost:8081/something)
  • Places a tray-icon that can be used to open the main URL (http://localhost:8081/something) or to stop the Http server.

How? With Java SE 6 is easy with its new Desktop and System Tray APIs. You can see sample source code of all these ideas.

When you are done you can distribute your application in a JAR executable file or you can wrap it into an exe file. Do you need a database? You can use Java DB included in Java SE 6 or other embedded database.

So… what are we doing? We are distributing a web application as a desktop application. What is this useful for?

  • Prototyping and demostrating: you can send your application to someone without more requirement that a standard Java SE 6 runtime installation.
  • Web applications working on-line and off-line. You can create two versions of your application sharing most of the code. One version will be a “desktop application” and the other one will run in a servlet container. If the user is off-line he can use the desktop version, and when he goes on-line the application can populate the changes to the server. So the desktop version can act as a data cache. The user can also work on-line in any workstation with the on-line version wich has the same user interface that the other one.
  • You want to create a desktop application and you love web design and development.
  • You want to create a mash-up desktop application using services such as Google Maps that are more easy to use with an HTML + CSS + JavaScript interface.

November 27, 2007

Introducing JAX-RS (JSR 311) and Jersey

JAX-RS is a new JSR (there is no final release yet). Jersey is its reference implementation. JAX-RS will be part of Java EE 6 and is intended to be used to create REST web services. It has an non-obtrusive and simple to understand API. Here you can find a brief example.

As you can see in the example you use annotations to map resources and methods to URIs (i.e. @UriTemplate(”/users”) ) and to indicate the content type of the response (i.e. @ProduceMime(”application/json”) ). Your methods can have any name and you don’t have to extend a certain class nor to implement any interface. So making unit testing is extreamly easy.

The HelloWorld example doesn’t demonstrate one of the great things about JAX-RS: the EntityProviders. They are responsible or reading and writing requests and responses as objects. Jersey comes with severeal useful Entity Providers and you can create your own ones. The Entity Providers that come with Jersey let you read and write:

  • JSON objects with Jettison and optinally annotated with JAXB. More information.
  • XML with JAXB.
  • ATOM and RSS feeds with ROME.
  • Strings, byte arrays and streams.
  • URL encoded request bodies (i.e. a request generated submitting a POST form from a browser).

You can also bind request parameters to method arguments. Example:

@HttpMethod("GET")
@UriTemplate("{userId}.txt")
@ProduceMime("text/plain")
public String sayHello(@UriParam("userId") String userId) {
    return "Hello " + userId;
}

Note how easy is to test this method.

Other thing I like a lot is that you don’t need a servlet container. This API is intented to be used for applications deployed in a J2EE container but you can make a main() method and use a lightweight Http server that comes with Java SE 6 (if you are using Java SE 5 there is an http.jar file in the jersey distribution with this lightweight Http server). So if you develop applications using this API the unit testing is extremely easy and you can also test them quickly without having a J2EE server running.

I have some cool ideas based on this API. I’ll tell you in future entries. By the moment you can see some example code.

Blog at WordPress.com.