我想抛出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;
}
}
网络服务实施。注意:我不想改变这里抛出的内容。
@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");
}
}
使用网络服务调用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>
使用网络服务调用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>
当使用网络服务调用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>
答案 0 :(得分:0)
您可以更改方法:
@WebMethod
public String throwCustomInnerException() throws MyCustomException {
throw new MyCustomException.CustomInner();
}
为:
@WebMethod
public String throwCustomInnerException() throws MyCustomException {
throw new MyCustomException(CustomInner.getClass().getSimpleName());
}