users@jersey.java.net

Re: [Jersey] Best practices - Web service and website

From: Paul Sandoz <Paul.Sandoz_at_oracle.com>
Date: Fri, 20 Aug 2010 16:38:38 +0200

On Aug 20, 2010, at 4:02 PM, Gary Moore wrote:

> Stephen,
>
> Our custom exceptions implement ExceptionMapper. see:
> http://github.com/fracturedatlas/parakeet/blob/master/src/main/java/org/fracturedatlas/athena/tix/exception/ParakeetException.java
>
> If you follow this pattern, perhaps you could pass the accept headers
> when you instantiate the Exception. Or, the resource layer could
> catch the Exception, inject the headers, and re-throw it. Then, the
> toResponse method could inspect the header and format a response
> accordingly.
>
> It's a little crude, and I don't much like inspecting the request
> heders like that because it takes away from the magic that Jersey is
> providing with PRoduces, Consumes, etc...
>

Yes, unfortunately exception mappers cannot be not scoped to the media
type with @Produces and nor does content negotiation occur for
exception mappers.

Using two URIs is not a nice solution in this regard.

I think the best way to resolve this without resorting to doing your
own conneg in the exception mappers is to support a common type for
the entity returned by the exception mapper. Then use
MessageBodyWriters (MBWs) for say XML/JSON and then a general one for
*/* that does not actually write anything, which should result in the
error pages of the web app being used (as no entity is written).

Another solution is to eschew error pages with the wep app and use an
extension of Viewable (which encapsulates the status code or Response)
with the reference to a JSP page and then use your own JSP templates
then have MBWs or XML or JSON. The XML/JSON MBWs should take
precedence other the general "*/*" MBW.

The latter seems quite appealing to me. And eventually i want to
extend the viewable support to work with XML and JSON-based templates.

Paul.




> This discussion from last year might help:
> http://jersey.576304.n2.nabble.com/Viewable-in-ExceptionMapper-td2356696.html
>
> On Fri, Aug 20, 2010 at 9:42 AM, Stephen Colebourne
> <scolebourne_at_joda.org> wrote:
>> I'm building a system where there are two distinct types of output -
>> web service and website.
>>
>> For the web service, I want exceptions to map to status codes. For
>> the
>> website, I want to map to error pages. Are there any strategies for
>> doing this? The exception mappers seem to be global.
>>
>> To make this work neatly, I have one class for the web service and
>> one
>> class for the website for the same resource. In order to get this to
>> work, I ended up adjusting the URI, so the web service one has /data
>> in the middle. Is it possible to have the same URI implemented in two
>> different classes (so long as the media type annotations are
>> different)?
>>
>> Stephen
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>>
>
>
>
> --
> Gary Moore
> http://www.gmoore.net
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>