users@jersey.java.net

[Jersey] how to see the errors when Jersey-Jackson-JAXB fails to parse some json

From: Carle, Bruno <Bruno.Carle_at_ca.com>
Date: Thu, 30 Dec 2010 09:47:34 -0000

Hello,

 

I am using jersey with jackson and jaxb to serve REST request, and I
would like to see the errors when JAXB fails to parse some json.

The default handler in JAXB unmarshaller context swallows all the
exceptions and they get silently ignored.

So I would like to replace the default handler when I am using Jaxb from
Jersey.

The only way I found to do this is by modifying the Jersey source code.

I am including it as it could be useful to other people, but also I
would like to know if there another way i.e. without modifying the
source?

 

In the JAXB unmarshaller context, the default event handler can be
replaced by calling setEventHandler()

The context is created In jersey by JSONJAXBContext.createUnmarshaller()

However JSONJAXBContext is final, and the Unmarshaler is instanciated
directly in createUnmarshaller()

 

So to log the events I modified the source of JSONJAXBContext, replacing
createUnmarshaller() by

 

    @Override

    public Unmarshaller createUnmarshaller() throws JAXBException {

                Unmarshaller retVal= new
JSONUnmarshallerImpl(jaxbContext, getJSONConfiguration());

                retVal.setEventHandler(new ValidationEventHandler() {

                                                @Override

                                                public boolean
handleEvent(ValidationEvent event) {

                                                

                                                                // first
of all log the validation error

                                                                if
(logger.isLoggable(Level.INFO))

 
logger.info("ValidationEventHandler: "+event);

                                                                

                                                                // This
is default behaviour, taken from
com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl

                                                                return
event.getSeverity()!=ValidationEvent.FATAL_ERROR; // ignore all but
FATAL_ERROR

                                                                  

                                                                //

                                                                // Using
the return statement below will change the behavior of Jersey

                                                                //
because returning false will raise an exception

                                                                //
return event.getSeverity()==ValidationEvent.WARNING; // ignore
only WARNINGs

                                                }

                                });

                return retVal;

    }

    static Logger
logger=Logger.getLogger(JSONJAXBContext.class.getName());

    

thanks

Bruno