users@jersey.java.net

Re: [Jersey] custom content negotiation

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Wed, 06 Aug 2008 10:22:25 +0200

Ryan Heaton wrote:
> Any tips on how to implement custom content negotiation?
>
> Jersey supports content negotiation via extensions, like this:
>
> /context/resource
> /context/resource.xml
> /context/resource.html
>
> What if I wanted to add content negotiation via URL, like this:
>
> /context/resource
> /context/xml/resource
> /context/html/resource
>

For a specific case you could have:

   @Path("resource")
   public MyRysource
   public MyResource resource() {
     ...
   }

   @Path("{suffix}/resource")
   public MyResource uriPathSegmentConneg(
       @PathParam("suffix") String suffix) {
     ...
   }

Note that when we support general regexes for @Path it may be possible
to unify the two sub-locator methods (but i would need to experiment
first to confirm, and to do that i need to implement it!).

For a generic case you could write a Jersey filter that checks the
penultimate path segment, and removes that from the URI path and adjusts
the Accept header accordingly. This of course means any URIs that are
built will not have the path segment associated with the media type (as
is the case when using suffixes).

Paul.

-- 
| ? + ? = To question
----------------\
    Paul Sandoz
         x38109
+33-4-76188109