users@jersey.java.net

First hurdles

From: Richard Wallace <rwallace_at_thewallacepack.net>
Date: Fri, 23 Nov 2007 21:19:33 -0800

Hello,

So I'm getting over my first hurdles using Jersey and just thought I'd
give a bit more feedback. After figuring out that the issue with the
scanning was because I was trying to run the webapp inplace with Jetty
my next challenge was actually getting it to do something. I like the
idea of having a provider to generate the response. But it's quite a
pain for it to have to be specified in a file in the META-INF/services
directory. Because of this I wound up having to split my webapp and my
REST classes into separate projects because otherwise I couldn't get the
dang things in the right place for the class loader to find it.

I understand there is some work going on to get better view handling
better, so let me second (or third or whatever I would be) getting
Freemarker integrated.

Something else I was wondering was what the best to declare a
MessageBodyWriter for a Collection of objects. Naturally I'd really
like to be able to just have a method in my resource like

@HttpMethod("GET")
@ProduceMime("text/plain")
public List<Bookmark> getBookmarks() {
    return ...;
}

and a provider like

@Provider
@ProduceMime("text/plain")
public class BookmarksProvider implements MessageBodyWriter<BookmarkList> {

    public boolean isWriteable(Class<?> clazz) {
        return List<Bookmark>.class.isAssignableFrom(clazz);
    }

    ...
}

Naturally that doesn't work because of erasures. So what I've instead
done is create a BookmarksList that implements List<Bookmark> and just
delegates to whatever actual List object is used to construct it. For
this simple spike it's pretty easy to do, but if my domain model was
larger managing all these extra List classes would be a major pain. I
don't know what the best solution is for this (other than pushing for
reified generic types that is), but maybe something as simple as adding
annotations to the resource method indicating what the MessageBodyWriter
or MessageBodyReader is for that method.

One other thing that I noticed in the HttpResponseAdaptor.commitAll()
method that the status code is set and then the MessageBodyWriter
provider is called. So, if there is an exception when creating the
response body, such as an exception when parsing a Freemarker template,
the client will get a 200 even though the response is going to be
incomplete, if not completely invalid. I understand that this would
most likely be caused by a bug in the template or provider, but it would
make tracking down the problem much easier if the client got a 500.

Now I'm off to play with getting Guice working as per Christians blog.
Wish me luck!

Rich