开发者

How do I extend a wsimport-generated exception annotated with WebFault?

I'm building a web service in Java from an existing WSDL. The wsimport tool has generated all the Java classes bound to the elements in the开发者_Python百科 service's schema. In particular, the fault declaration yields the following class:

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

Now I'd like to extend this class, so I can add more behavior:

public class MyServiceFault extends ServiceFault {
    // some behavior
}

When I now throw instances of MyServiceFault from my application, I expect these errors to be properly serialized to XML in the SOAP answer. But instead, I get something like this:

<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>

That is, I am completely missing the faultInfo element. My SOAP stack treats MyServiceFault as any other exception, not as an exception representing a fault in the service.

I thought first it was because the @WebFault annotation wasn't inherited by MyServiceFault, but I've tried again after explicitly adding this annotation, without success.

Any idea what I am doing wrong here?


For what it's worth, I've implemented it this way.

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;
    }
}

which produces something like:

<?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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜