我想抛出MyCustomException
的子类,但要通过Web服务传输超类;但是,子类被转移了。我尝试将注释WebFault
添加到类中,但没有效果。我提供了一个当前正在发生的事情的例子,以及一个我想发生的事情的例子。
例外。
public class MyCustomException extends Exception {
String text;
public static class CustomInner extends MyCustomException {
public CustomInner1() {
super("inner");
}
}
public MyCustomException(String text) {
this.text = text;
}
public String getText() {
return text;
}
}
Web 服务实现。注意:我不想改变这里的内容。
@Stateless(name = "MyService", mappedName = "MyService")
@LocalBean
@WebService(targetNamespace = "http://my.org/ns/")
public class MyService {
@WebMethod
public String throwCustomInnerException() throws MyCustomException {
throw new MyCustomException.CustomInner();
}
@WebMethod
public String throwCustomException() throws MyCustomException {
throw new MyCustomException("text");
}
}
用于使用 Web 服务进行throwCustomException()
调用的 XML。
<?xml version='1.0' encoding='UTF-8'?>
<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>pack.MyCustomException</faultstring>
<detail>
<ns2:MyCustomException xmlns:ns2="http://my.org/ns/">
<text>text</text>
</ns2:MyCustomException>
</detail>
</S:Fault>
</S:Body>
</S:Envelope>
用于使用 Web 服务进行throwCustomInnerException()
调用的 XML。
<?xml version='1.0' encoding='UTF-8'?>
<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>pack.CustomInner</faultstring>
</S:Fault>
</S:Body>
</S:Envelope>
当使用 Web 服务调用throwCustomInnerException()
时,我想发生的是以下内容:
<?xml version='1.0' encoding='UTF-8'?>
<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>pack.MyCustomException</faultstring>
<detail>
<ns2:MyCustomException xmlns:ns2="http://my.org/ns/">
<text>inner1</text>
</ns2:MyCustomException>
</detail>
</S:Fault>
</S:Body>
</S:Envelope>
您可以更改方法:
@WebMethod
public String throwCustomInnerException() throws MyCustomException {
throw new MyCustomException.CustomInner();
}
自:
@WebMethod
public String throwCustomInnerException() throws MyCustomException {
throw new MyCustomException(CustomInner.getClass().getSimpleName());
}