简体   繁体   中英

JAVA | CXF | JAX-RS | Exception handling

We are using cxf version 2.5.2 and we expose and consume couple of restful web services using cxf jaxrs.

Any idea how to throw exceptions from server to client ? I tried defining a custom exception mapper by implementing ExceptionMapper interface (toResponse method) and added the bean in cxf jaxrs:server providers list.

Client side : Implemented ResponseExceptionMapper (fromResonse method) and added the bean in cxf jaxrs:client providers list. But this doesnt seem to work.

Exception is a custom exception that extends java.lang.Exception. Got "IllegalAnnotationsException 2 counts of IllegalAnnotationExceptions StackTraceElement does not have a no-arg default constructor"

Found http://java.net/jira/browse/JAXB-814 and upgrading to jaxb-impl 2.2.5 resolved that issue.

But at the end, i am struck with the following exception in server side (during client invocation) :

org.apache.cxf.jaxrs.provider.AbstractJAXBProvider :
com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions blueprints.common.util.BlueprintsException does not have a no-arg default constructor"

and in client side :

JAXBException occurred : 1 counts of IllegalAnnotationExceptions. 
java.lang.ClassCastException: org.apache.cxf.helpers.LoadingByteArrayOutputStream$1 cannot be cast to  myExceptionClass:java.lang.ClassCastException" when i typecast Response.getEntity()) tp myExceptionClass in fromResponse (jaxrs provider that implements ResponseExceptionMapper) 

Has anybody else faced similar issues? Do we need to add custom out interceptors ? The cxf doc doesnt provide example for exception handling using spring config.

Edit based on jigar's comment: After adding default constructor, the entity in response contains

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<blueprintsServiceException>
<stackTrace/>
<stackTrace/>
<stackTr‌​ace/>
<stackTrace/>
...
</blueprintsServiceException>` 

and i still get java.lang.ClassCastException:

org.apache.cxf.helpers.LoadingByteArrayOutputStream$1 cannot be cast to blueprints.server.exception.BlueprintsServiceException' when i typecast Response.getEntity()) to myExceptionClass in fromResponse 
(jaxrs provider that implements ResponseExceptionMapper) 

Thanks,
Gayathri

in BlueprintsException you need to provide a default constructor,

You might have overloaded the constructor which hides the default constructor, So you need to provide default constructor explicitly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM