tags:

views:

154

answers:

2

Forgive me if I am asking an obvious question (maybe I missed it in the docs somewhere?) but has anyone found a good way to organize their URLs in Jersey Java framework?

I mean organizing them centrally in your Java source code, so that you can be sure there are not two classes that refer to the same Url.

For example django has a really nice regex-based matching. I was thinking of doing something like an enum:

enum Urls{
    CARS  ("cars"),
    CAR_INFO ("car", "{info}");

    public Urls(String path, String args) 
    ...
}

but you can imagine that gets out of hand pretty quickly if you have urls like:

cars/1/wheels/3

where you need multiple path-ids interleaved with one another...

Any tips?

+2  A: 

From my experiences with Jersey, when I tried to annotate two places with the same @Path, I had compilation errors and it wouldn't run. This might not always be the case, so the following might help:

You can get an application.wadl file from your Jersey app by simply requesting it from you web resource:

$ curl http://localhost:8080/application.wadl

or if you prefixed your web services under /ws/

$ curl http://localhost:8080/ws/application.wadl

The application.wadl file is an XML file that shows you all of your resources in your running application, as well as what methods you can call on a resource. See the following resource on how this file is laid out.

Nick Klauer
Thanks for letting me know about wadl.
drozzy
Looks like there really is no good way to organized URLs in jersey. In any case - thanks!
drozzy
A: 

Well - I assume you have a @Path on each resource? This means you don't have to keep track of URLs across your entire application, rather just within each class - which is relatively easy if you annotate the interface.

Using enums won't work - you can only put contants into an annotation, but using a class holding final static String could work.

public class UrlConst {
  public final static RESOURCE_MY_RESOURCE="/resource";
  public final static RESOURCE_MY_RESOURCE2="/resource";
  public final static OP_GET_ALL="/";
  public final static OP_GET_BY_ID="/{id}";
}

@Path(UrlConst.RESOURCE_MY_RESOURCE)
public interface MyResource {

 @GET
 @Path(UrlConst.OP_GET_ALL)
 @Produces(MediaType.APPLICATION_XML)
 public ObjectList getAll();

 @GET
 @Path(UrlConst.OP_GET_BY_ID)
 @Produces(MediaType.APPLICATION_XML)
 public Object get(@PathParam("id") int id);

}

@Path(UrlConst.RESOURCE_MY_RESOURCE2)
public interface MyResource2 {

 @GET
 @Path(UrlConst.OP_GET_ALL)
 @Produces(MediaType.APPLICATION_XML)
 public ObjectList getAll();

 @GET
 @Path(UrlConst.OP_GET_BY_ID)
 @Produces(MediaType.APPLICATION_XML)
 public Object get(@PathParam("id") int id);

}

etc.

Robert Wilson
Woot, thanks for letting me know about javax.ws.rs.core.MediaType constant! I was using my own :-)
drozzy
About your answer - I see what you are saying, but it looks a bit clumsy to me. I'll think about it, thanks.
drozzy