Jersey samples

The samples supplied with Jersey are on the java.net maven repository as a single zip file or individually (browse from here).

All the of samples are capable of being executed from the command line or from NetBeans 6.5 beta/dev build (with the maven plug-in configured to an external maven distribution version 2.0.9 or greater). Maven makes it easy to distribute the samples without redistributing third party dependencies, such as Grizzly, GlassFish embedded, Spring, and Scala, some of which can be rather large.

Some samples have unit tests that avail of the Jersey client API. This serves a number of purposes:

  • How to use the Jersey client API;
  • How to deploy in embedded scenarios using Grizzly and GlassFish;
  • How to develop functionality tests for resources using JUnit; and
  • Limit or remove manual testing of the samples.

We plan to automate the testing of all samples and from this experience we plan to create a useful JAX-RS/Jersey testing framework that is capable of abstracting from the actual HTTP container used to deploy resource classes. The ultimate goals are to make it easier to test and to automate everything so that we can test continuously via Hudson. Potentially we can then spin stable releases as fast as Hudson slaves can test.

A sample i particularly like and i recently added is Sparklines, which was inspired by Joe Gregorio's Sparklines service and python implementation. Sparklines, "as defined by Tufte, are intense, simple, word-sized graphics".

Here is a snippet of the Sparklines resource class:

@Path("/")
@Produces("image/png")
public class SparklinesResource {
    List<Integer> data;

    @DefaultValue("20") @QueryParam("height") int imageHeight;

    Interval limits;

    EntityTag tag;

    public SparklinesResource(
            @QueryParam("d") IntegerList data,
            @DefaultValue("0,100") @QueryParam("limits") Interval limits,
            @Context Request request,
            @Context UriInfo ui) {
        if (data == null)
            throw new WebApplicationException(400);
        
        this.data = data;
        
        this.limits = limits;

        if (!limits.contains(data))
            throw new WebApplicationException(400);

        this.tag = computeEntityTag(ui.getRequestUri());
        if (request.getMethod().equals("GET")) {
            Response.ResponseBuilder rb = request.evaluatePreconditions(tag);
            if (rb != null)
                throw new WebApplicationException(rb.build());
        }
    }

Besides being a fun application with a practical use it uses a whole bunch of JAX-RS features in one Java class: query parameter with default values, application-specific classes for query parameters, parameters on fields, throwing exceptions in constructors, and preconditions where entity tags are generated from the information in the request URI.

For those wanting to move beyond 1.0 i have just added a simple Groovy sample to the 1.0.1-SNAPSHOT. It needs to be improved and I ain't that familiar with Groovy so if any one wants to contribute something more substantial to show off Groovy, JAX-RS and Jersey let me know.

And... on a tangential topic related to languages i have been taking a little look at Clojure. There is a very thought provoking presentation on line. For those of us not that familiar Lisp the syntactical "explosion" of braces of Clojure can be a bit overwhelming compared to Java at first, but, if one looks at Clojure's Java interoperation and then compares Clojure code with Java code with the following consideration, think of curly braces as just braces, then the Clojure syntax is OK and can be very conscise yet still readable, and it is often just the order of where "." and braces occur that changes. I also took a little look at the code, for what Clojure does the set Java code is rather small, often strange (as Rich Hickey says he tries to apply functional idioms to Java code), and has some very interesting implementatons of persistent (in the functional sense) data structures.

Comments:

I am interested in using Jersey with the Berkeley DB XML as a backend running in Glassfish. And, just to make it interesting, I am writing in Scala. And I don't know what I'm doing. Fun.

Do you by any chance have a working example of Jersey using Scala that will just load up in NetBeans 6.5 and work with Glassfish?

I tried downloading your previous Scala samples but I could not get them to work. Such an example would really help to get me started, and probably others, too.

Posted by Charles Munat on November 21, 2008 at 04:57 PM CET #

Hi Charles,

Always fun exploring new territories :-)

The samples should run fine with NetBeans 6.5 using the maven plugin and also setting it up to utilize an external maven installation, version 2.0.9.

I tried to include Scala-based unti tests, but there was a bug in the Scala version preventing me from completing this. A new release is available so i should revisit this.

The Scala example uses the Grizzly embedded server, but it should be easy to modify by copying the pattern for the helloworld-webapp example.

I will look into providing another simple scala web app sample.

BTW if you have any further detailed questions about Scala/Jersey i recommend you email:

mailto:users@jersey.dev.java.net

Paul.

Posted by Paul Sandoz on November 24, 2008 at 04:01 AM CET #

My cup runneth over! Thank you, Paul, for your concise and helpful response. I'll follow your recommendations, and will keep an eye posted for another simple scala web app sample.

Posted by Charles Munat on November 24, 2008 at 10:29 AM CET #

A scala-based web-app example is now added:

http://download.java.net/maven/2/com/sun/jersey/samples/scala-helloworld-webapp/1.0.1-SNAPSHOT/scala-helloworld-webapp-1.0.1-SNAPSHOT-project.zip

Note that because of a bug in the Scala 2.7.2 compiler it is not possible to utilize the client API.

Paul.

Posted by Paul Sandoz on November 26, 2008 at 06:54 AM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
About

sandoz

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today