users@jersey.java.net

[Jersey] Re: Mapping exceptions thrown from JaxB bean methods

From: Yegor Yarko <yaegor_at_gmail.com>
Date: Tue, 14 Feb 2012 16:12:22 +0100

Pavel,

Thank you for the reply.

Yes, the exception is thrown during marshalling in my case (resource method
returns a JaxB-annotated bean, whose method later throws an exception).

3.3.4 seems to cover exception thrown from a resource method which is not
the case for me.

However it is now more clear to me: It seems that the reason of not mapping
exceptions is probably that it cannot be reported duly anyway: part of the
response might have been already sent to the client with all the HTTP
headers, so there is no way to report an error in the middle of the
response rendering.

Seems that means that using JaxB-annotated methods that can throw
exceptions (as opposed to fields) is not a good idea.

Best regards,
Yegor

On Wed, Feb 8, 2012 at 17:49, Pavel Bucek <pavel.bucek_at_oracle.com> wrote:

> Hello,
>
> I think main problem here is when is that exception thrown - is it during
> marshalling/unmarshalling? (MessageBodyReader/Writer invocation).
>
> There might be a problem, because we could end up in infinite loop if we'd
> let handle exception mappers exceptions thrown during serializing a
> response.
>
> Additionally, see chapter 3.3.4, bullet 2 in JAX-RS spec:
>
>
> 1.
>
> Unchecked exceptions and errors MUST be re-thrown and allowed to
> propagate to the underlying container.
>
> So to answer - its by design. If you can, share additional details of your
> app and what do you want to achieve and we might be able to give a better
> advice.
>
> Regards,
> Pavel
>
>
>
> On 2/2/12 9:18 PM, yaegor_at_gmail.com wrote:
>
> I have an ExceptionMapper for my custom exception (which extends
> RuntimeException).
> When my exception is thrown from a resource method (annotated with
> @Path ), it is correctly mapped through my instance of ExceptionMapper.
>
> However, when my exception is thrown from @XmlAttribute-annotated
> method in a JaxB bean (which in turn is returned by a resource method),
> no mapping occurs.
>
> Is this by design?
>
>
> The current way to affect errors returned from JaxB methods seems to
> wrap them into WebApplicationException which seems sub-optimal.
>
> Is there any other way to map the exceptions from JaxB bean methods?
>
>
>
>