users@jersey.java.net

Re: [Jersey] setting JAXB marshall/unmarshall error response message

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Wed, 05 Aug 2009 08:46:32 +0200

Hi,

You can use an ExceptionMapper [1] to map WebApplicationException and
check for a 400 status and also check if the cause is a JAXB
validation exception, if so you can modified the Response instance to
contain an entity with more information, otherwise you can return the
Response as is.

For example,

   @Provider
   public class MyMapper implements
ExceptionMapper<WebApplicationException> {
     public Response toResponse(WebApplicationException ex) {
       if (ex.getResponse().getStatus() == 400 &&
ex.getCause().getClass() == / JAXB exception/) {
         ...
       } else {
         return ex.getResponse();
       }
     }
   }

The AbstractListElementProvider provides support for Collection/
List<T> and T[] where T is a JAXB bean.

The AbstractJAXBElementProvider provides support for JAXBElement<T>.


I think to clarify this area (another user wanted to do the same thing
as you) we need to define a JAXBWebApplicationException which is
thrown and can be mapped more specifically that any
WebApplicationException.

Paul.

[1] https://jsr311.dev.java.net/nonav/javadoc/javax/ws/rs/ext/ExceptionMapper.html

On Aug 5, 2009, at 1:52 AM, Bao,Sam wrote:

> So I’ve got my own provider for validating of application/xml
> requests and it’s doing validation fine when everything validates.
> My question is how do I set the response message body if the
> validation should fail. Currently it just returns a message saying
> the server returned a 400 error, but I would like to also spit back
> the actual validation error so I can know not just the fact the
> request is wrong, but what’s wrong with the request. The
> WebApplicationException on line 99 of AbstractRootElementProvider
> class has this info, but it gets lost eventually in
> WebApplicationImpl line 1066 where the response exception is just
> not used. This is the stack at which happens:
>
> WebApplicationImpl.onException(Throwable, Response,
> HttpResponseContext) line: 1051
> WebApplicationImpl
> .mapWebApplicationException(WebApplicationException,
> HttpResponseContext) line: 1020
> WebApplicationImpl
> .mapMappableContainerException(MappableContainerException,
> HttpResponseContext) line: 1004
> WebApplicationImpl._handleRequest(WebApplicationContext,
> ContainerRequest, ContainerResponse) line: 715
> WebApplicationImpl.handleRequest(ContainerRequest,
> ContainerResponse) line: 667
> WebApplicationImpl.handleRequest(ContainerRequest,
> ContainerResponseWriter) line: 658
> HttpHandlerContainer.handle(HttpExchange) line: 180
>
> So how can I get the validation error to return in the response
> body? Creating a provider that extends AbstractRootElementProvider
> wouldn’t matter because that’s actually throwing the exception,
> right? So not sure what to do here.
>
> Also, I noticed theres AbstractListElementProvider and
> AbstractJAXBElementProvider, and I’m not sure when they’re used.
> There’s no class level javadoc, and through all the requests I’ve
> sent using unit tests, I have yet to step through those classes.
>
> Any hints and tips would be greatly appreciated. Thanks.
> CONFIDENTIALITY NOTICE This message and any included attachments are
> from Cerner Corporation and are intended only for the addressee. The
> information contained in this message is confidential and may
> constitute inside or non-public information under international,
> federal, or state securities laws. Unauthorized forwarding,
> printing, copying, distribution, or use of such information is
> strictly prohibited and may be unlawful. If you are not the
> addressee, please promptly delete this message and notify the sender
> of the delivery error by e-mail or you may call Cerner's corporate
> offices in Kansas City, Missouri, U.S.A at (+1) (816)221-1024.