Java | CXF | JAX-RS | 异常处理

3
我们正在使用cxf版本2.5.2,并使用cxf jaxrs公开和使用了几个restful web服务。
有没有办法从服务器向客户端抛出异常? 我尝试通过实现ExceptionMapper接口(toResponse方法)定义自定义异常映射器,并将bean添加到cxf jaxrs:server提供程序列表中。
客户端:实现ResponseExceptionMapper(fromResonse方法),并将bean添加到cxf jaxrs:client提供程序列表中。但是这似乎行不通。
异常是扩展java.lang.Exception的自定义异常。 得到“IllegalAnnotationsException 2 counts of IllegalAnnotationExceptions StackTraceElement does not have a no-arg default constructor”
发现http://java.net/jira/browse/JAXB-814,升级到jaxb-impl 2.2.5解决了这个问题。
但最后,在服务器端(在客户端调用期间)遇到以下异常:
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"

在客户端:
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) 

有其他人面临类似的问题吗? 我们需要添加自定义输出拦截器吗?cxf文档没有提供使用spring配置处理异常的示例。

根据jigar的评论进行编辑: 添加默认构造函数后,响应中的实体包含

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

我仍然收到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) 

谢谢,
Gayathri

1个回答

0
BlueprintsException中,您需要提供一个默认构造函数,
您可能已经重载了隐藏默认构造函数的构造函数,因此需要显式提供默认构造函数。

感谢您的回复。在添加默认构造函数后,响应中的实体包含'<?xml version="1.0" encoding="UTF-8" standalone="yes"?><blueprintsServiceException><stackTrace/><stackTrace/><stackTrace/><stackTrace/>...</blueprintsServiceException>',但我仍然会得到java.lang.ClassCastException: org.apache.cxf.helpers.LoadingByteArrayOutputStream$1无法转换为blueprints.server.exception.BlueprintsServiceException'的错误,当我将Response.getEntity()强制转换为fromResponse中的myExceptionClass时(jaxrs提供程序实现了ResponseExceptionMapper)。 - crankparty

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接