Hello,
First off, I am loving Jersey! I'm finding it very intuitive and
clean to write my resources/providers. Keep up the good work.
My application has 5-10 "standard" rest resources that lend themselves
well to the class/method annotation style of Jersey. However, several
more resources are essentially defined externally. Think of these as
XML reports which define the url they want to publish to:
Report #1:
<resource url = "/widgets/{widgetType}/widgetsInNeedOfRepair">
.... DSL describing how to execute the query
<resource>
Report #2:
<resource url =
"/company/{company}/people/{department}/peopleWithExcessVacationTime?employeeType=manager">
.... DSL describing how to execute the query
<resource>
These reports can be executed by the same generic Resource class, but
many different Paths need to match this generic class. My goal is to
allow this generic Resource to take advantage of other annotations
(Produces, GET, etc) and all of the other Jersey resources such as
Providers.
I found a recommendation to use
@Path("{fullPath}")
I can then use UriInfo#getPathParameters to inspect what was passed in
and glean out the path Parameters. I only have two problems w/ this
approach:
1. As far as I can tell, I'd need a method for each number of
anticipated path elements:
@GET
public Iterable<ExportNode> executeQuery1(@Context UriInfo ui) {
return executeQuery(ui);
}
@GET
@Path("/{param1}")
public Iterable<ExportNode> executeQuery2(@Context UriInfo ui) {
return executeQuery(ui);
}
@Path("/{param1}/{param2}")
@GET
public Iterable<ExportNode> executeQuery3(@Context UriInfo ui) {
return executeQuery(ui);
}
etc etc
2. It seems this all-encompassing URL matching would cause problems
for static file serving.
I'm so new to Jersey and even Rest that I'm sure I'm taking a
completely wrong approach here, but I think my "dream" code would be:
... at startup:
jersey.registerResource("/widgets/{widgetType}/widgetsInNeedOfRepair",
GenericResource.class);
jersey.registerResource("/company/{company}/people/{department}/peopleWithExcessVacationTime?employeeType=manager",
GenericResource.class);
...and Generic Resource:
@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
public class GenericResource {
@GET
public ReportResult executeQuery(@Context UriInfo ui) {
//use UriInfo to figure out exactly which report they are invoking,
and what the parameters are
}
}
What's the right approach for a problem like this? Any input is much
appreciated.
Thanks,
Adam