Apache CXF and Variable Paths

In most situations, a REST service would be set up with a concrete path and known number of path elements. On occasion, however, we may need to implement a service with a variable path. Maybe it is for a series of search parameters or, as is my case, an actual file path needs to be provided. There is great support for the known paths. You just type it in the Path annotation and tell it where the variables are. Easy. Well, for unknown paths things get a little hairier, although the solution is still simple and not as ugly as it could be. Let's look at an example for encoding a URL that looks like this:   http://codingfrontier.blogspot.com/subsystem/get/path/to/file/somewhere/in/the/system As always, we start with our Path annotation. We have already set up the path for "subsystem" at the class level, so on the method that we want to receive the multi-element parameter we need to set our Path annotation to accept the rest of the URL, whatever that may be, and do something a little dirty in our receiving method.   Path("get/.*")   public void get(@Context UriInfo uriInfo) {     String pathParam = uriInfo.getPath().replaceAll(".*/get/","");     ...   } Calling uriInfo.getPath() will give us the entire path, so we are using a simple regex to strip the constant portion off the beginning of the string.  This will provide us with the entire rest of the string to do with as we please. Accessing the UriInfo this way is less than desirable, but in this case it gets the job done. If you need a cleaner way of accessing each parameter without having to parse them yourself, you can get to them directly as well.   public void get(@Context UriInfo uriInfo) {     String firstParam = uriInfo.getPathSegments().get(0).getPath();     ...   } It isn't very elegant, but it gives you the flexibility to write some really fancy REST services with dynamic URLs.  I don't normally like to step out of the framework like that, but it is nice to have the flexibility to do so when the need arises.