简体   繁体   English

如何扩展用 WebFault 注释的 wsimport 生成的异常?

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

I'm building a web service in Java from an existing WSDL.我正在从现有的 WSDL 用 Ja​​va 构建 Web 服务。 The wsimport tool has generated all the Java classes bound to the elements in the service's schema. wsimport工具已生成绑定到服务模式中的元素的所有 Java 类。 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.当我现在从我的应用程序中抛出MyServiceFault实例时,我希望这些错误能够在 SOAP 答案中正确序列化为 XML。 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 .也就是说,我完全错过了 faultInfo 元素 My SOAP stack treats MyServiceFault as any other exception, not as an exception representing a fault in the service.我的 SOAP 堆栈将MyServiceFault视为任何其他异常,而不是表示服务中出现故障的异常。

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.我首先认为是因为@WebFault注释没有被MyServiceFault继承,但是在明确添加此注释后我再次尝试,但没有成功。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM