2009-12-17 81 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>