2009-12-17 40 views
1

我從現有的WSDL構建Java中的Web服務。 wsimport工具已生成綁定到服務模式中的元素的所有Java類。特別是,故障申報產生了以下類:如何擴展由WebFault註釋的wsimport生成的異常?

@javax.xml.ws.WebFault(name = "Fault", targetNamespace = "http://my.company.com/service-1") 
public class ServiceFault extends java.lang.Exception { 
    // constructors and faulInfo getter 
} 

現在我想延長這一類,所以我可以添加更多的行爲:

public class MyServiceFault extends ServiceFault { 
    // some behavior 
} 

當我從現在扔MyServiceFault實例我的應用程序,我希望這些錯誤能夠在SOAP答案中正確地序列化爲XML。但是,相反,我得到的是這樣的:

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"> 
    <env:Header/> 
    <env:Body> 
    <env:Fault> 
     <faultcode>env:Server</faultcode> 
     <faultstring>Some fault string.</faultstring> 
    </env:Fault> 
    </env:Body> 
</env:Envelope> 

也就是說,我完全錯過了faultInfo元素。我的SOAP堆棧將MyServiceFault視爲任何其他異常,而不是表示服務中的故障的異常。

我以爲這是因爲@WebFault註釋沒有被MyServiceFault繼承,但是我在明確添加此註釋之後再次嘗試,但沒有成功。

任何想法我在做什麼錯在這裏?

回答

0

對於它的價值,我已經用這種方法實現了。

import javax.xml.ws.WebFault; 

@WebFault(name = "SomeException") 
public class SomeException extends Exception { 

    private FaultBean faultInfo; 

    public SomeException(String message, FaultBean faultInfo) { 
     super(message); 
     this.faultInfo = faultInfo; 
    } 

    public SomeException(String message, FaultBean faultInfo, 
      Throwable cause) { 
     super(message, cause); 
     this.faultInfo = faultInfo; 
    } 

    public FaultBean getFaultInfo() { 
     return faultInfo; 
    } 
} 

產生類似:

<?xml version="1.0" ?> 
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> 
<S:Body> 
<S:Fault xmlns:ns4="http://www.w3.org/2003/05/soap-envelope"> 
<faultcode>S:Server</faultcode> 
<faultstring>SomeErrorString</faultstring> 
<detail> 
<ns2:SomeException xmlns:ns2="http://namespace/"> 
<message>SomeErrorMessage</message> 
</ns2:SomeException> 
</detail> 
</S:Fault> 
</S:Body> 
</S:Envelope>