users@jersey.java.net

[Jersey] Implicit handling of all OPTIONS request

From: Go Hori <go_at_livemocha.com>
Date: Tue, 29 Nov 2011 10:51:56 -0800

Hello Jersey experts.

I have yet another problem I have encounter for which I hope there is a
better solution than I came up with.

Jersey handles OPTIONS and HEAD requests automatically unless a method
is implicitly implemented.
I found out that I need to implement OPTIONS method for all methods I have
unique @Path.

It's easier with an example. Let's say I have two methods like the
followings to begin with.

@GET
@Path("foo")
public Response foo() {
}
@GET
@Path("{path: .*}")
public Response all() {
}

Then, I want to handle OPTIONS request for any path, so I add something
like this hoping OPTIONS /anything will go to this method.
@OPTIONS
@Path("{path: .*}")
public Response allOptions() {
}

But, it doesn't. A request to OPTIONS /bar will go to the method, but
OPTIONS /foo won't because Jersey will find default Options handler for a
path "foo".
This makes sense. "foo" is a better match, and Jersey is finding a default
Options handler for "foo" path.

So, then I have to have this in order for me to handle the request.
@OPTIONS
@Path("foo")
public Response fooOptions() {
}

Now, my question is, "is there a better way to do this other than to have
corresponding OPTIONS handler for all the @Path I am handling in a
resource?".
It seems like there is ought to be a better way that I don't know of.

Thanks in advance,
Go