Hi Robert
...just an update, looks like what's happening is that during an unmarshalling error, the unmarshaller eats the exception and
returns null. CXF then tries to invoke the service with the null result (sun.reflect.NativeMethodAccessorImpl.invoke0(Native
Method)) which causes an NPE which is then wrapped in an xml fault response.
So my question remains the same.
How can I format the fault response in JSON format?
One option is to create a custom MessageBodyReader. You can copy and paste the existing JSONProvider and in the exception clause
throw a WebApplicationException with a customized Response.
Furthermore how could I have caught the unmarshall exception earlier and
handled it myself?
Unfortunately, a default JSONProvider (and a JAXB one) are broken in how they handle the exceptions, they basically do nothing but
printing the stack trace and returning null. According the spec they should throw WebApplicationException instead. If it were the
case then you'd be able to provide a JAX-RS ExceptionMapper for WebApplicationException. I'll fix it.
So for the moment the only workaround is to provide a custom message body
handler...
Cheers, Sergey
Thanks,
-Robert
----- Original Message ----
From: Wong <[EMAIL PROTECTED]>
To: [email protected]
Sent: Sunday, September 28, 2008 10:35:51 AM
Subject: Re: How can I get a Fault Response in JSON format?
Hi Sergey,
Yup, JAX-RS via annotations.
Thanks,
-Robert
----- Original Message ----
From: Sergey Beryozkin <[EMAIL PROTECTED]>
To: [email protected]
Sent: Sunday, September 28, 2008 10:01:26 AM
Subject: RE: How can I get a Fault Response in JSON format?
Are you using JAX-RS ?
Cheers, Sergey
-----Original Message-----
From: Wong [mailto:[EMAIL PROTECTED]
Sent: 27 September 2008 02:20
To: [email protected]
Subject: How can I get a Fault Response in JSON format?
Is there a way to get Fault messages in JSON? Right now, I have a service
that tries to consume (and produce) a JSON message. However if there is an
UnmarshalException while tryng to bind the incoming message, an XML fault
code message is returned (<ns1:XMLFault...). This can be bad if the caller
was expecting JSON.
A more general question would be how can I customize return messages even if
an error happens upstream of my actual service?
Thanks!
-Robert
----------------------------
IONA Technologies PLC (registered in Ireland)
Registered Number: 171387
Registered Address: The IONA Building, Shelbourne Road, Dublin 4, Ireland
----------------------------
IONA Technologies PLC (registered in Ireland)
Registered Number: 171387
Registered Address: The IONA Building, Shelbourne Road, Dublin 4, Ireland