2012-03-13 90 views
2

我們使用的是cxf版本2.5.2,並且我們使用cxf jaxrs公開和使用了幾個寧靜的web服務。JAVA | CXF | JAX-RS |異常處理

任何想法如何拋出從服務器到客戶端的異常? 我試着通過實現ExceptionMapper接口(toResponse方法)定義一個自定義異常映射器,並在cxf jaxrs:server providers列表中添加了這個bean。

客戶端:實現ResponseExceptionMapper(fromResonse方法)並在cxf jaxrs:client providers列表中添加bean。但這似乎並不奏效。

異常是擴展java.lang.Exception的自定義異常。 了「IllegalAnnotationsException 2項IllegalAnnotationExceptions的StackTraceElement的罪名沒有一個無參數的默認構造函數」

發現http://java.net/jira/browse/JAXB-814和升級,以解決這一問題JAXB-implement執行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

回答

0

in BlueprintsException您需要提供默認構造函數,

你可能已經超載,它可以隱藏默認構造函數的構造,所以,你需要提供默認的構造函數明確

+0

感謝您的答覆。添加默認構造函數後,響應中的實體包含'<?xml version =「1.0」encoding =「UTF-8」standalone =「yes」?> ...獲取 java.lang.ClassCastException:org.apache.cxf.helpers.LoadingByteArrayOutputStream $ 1無法轉換爲blueprints.server.exception.BlueprintsServiceException(當我將類型Response.getEntity())轉換爲fromResponse中的myExceptionClass(實現ResponseExceptionMapper的jaxrs提供程序) – crankparty 2012-03-13 11:46:18

+0

請更新問題 – 2012-03-13 11:53:23

+0

完成。編輯的問題。 – crankparty 2012-03-13 12:12:08