有一个问题。我从wsdl生成了一个WS服务器,我正试图了解异常处理。基本上在WSDL中定义了一个名为Throwable_Exception的自定义异常。现在我已经尝试测试SOAP响应中的错误,它工作了,这就是结果:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<soap:Fault>
<faultcode>soap:Server</faultcode>
<faultstring>TEST Custom Exception</faultstring>
</soap:Fault>
</soap:Body>
</soap:Envelope>
"faultstring"标记是我在异常中抛出的消息。现在我的目标是在响应中发送这样的信息:
- 关于异常的可读消息
- 服务器抛出的异常
- 最终异常的详细信息
我在Oracle的指南中看到SOAP错误信封应该是这样的:
<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>Your name is required.</faultstring>
<detail>
<ns2:MissingName xmlns:ns2="http://examples/">
<message>Your name is required.</message>
</ns2:MissingName>
<ns2:exception xmlns:ns2="http://jax-ws.dev.java.net/"
class="examples.MissingName" note="To disable this feature, set
com.sun.xml.ws.fault.SOAPFaultBuilder.disableCaptureStackTrace system
property to false">
<message>Your name is required.</message>
<ns2:stackTrace>
<ns2:frame class="examples.HelloWorld" file="HelloWorld.java"
line="14" method="sayHelloWorld"/>
...
</ns2:stackTrace>
</ns2:exception>
</detail>
</S:Fault>
</S:Body>
</S:Envelope>
问题是,在我的代码中,正如你所看到的,它没有显示"detail"标签。你们知道如何在SOAP响应中显示这类信息吗?
public String myMethod(String field1, String field2) throws Throwable_Exception {
LOG.info("Executing operation myMethod");
try {
java.lang.String _return = "";
List<String> testExceptions = new ArrayList<>();
testExceptions.get(0);
return _return;
} catch (IndexOutOfBoundsException ex) {
throw new Throwable_Exception("TEST Custom Exception", ex.getCause());
}
//throw new Throwable_Exception("Throwable...");
}
在impl类中,我试图强制一个IndexOutOfBoundsException,并试图使用这个构造函数:
public Exception(String message, Throwable cause) {
super(message, cause);
}
和我认为这种方法可以解决问题,但显然不是。
注意:WS部署在WildFly18上。
对于仍然在寻找答案的每个人来说,我已经理解,基本上,如果您有一个xsd模式,您必须定义异常细节的复杂类型。例如:
<xs:complexType name="YourCustomException">
<xs:sequence>
<xs:element name="exName" type="xs:string" minOccurs="0"></xs:element>
<xs:element name="exMessage" type="xs:string" minOccurs="0"></xs:element>
<xs:element name="exDetails" type="xs:string" minOccurs="0"></xs:element>
</xs:sequence>
</xs:complexType>
在Java中,您将拥有来自这个complexType和他的异常类的类。在异常类中,您必须带来complexType的属性(如果您使用CXF或其他一些工具生成类,一切都会得到处理),并且您必须找到在customException中实例化complexType的首选方法,因此在SOAP Fault中"详细信息"。
Java中CustomException的例子:
@WebFault(name = "YourCustomException", targetNamespace = "http://serviceendpoint/")
public class YourCustomException_Exception extends Exception {
private it.your.package.YourCustomException YourCustomException;
public YourCustomException_Exception() {
super();
}
public YourCustomException_Exception(String message) {
super(message);
}
public YourCustomException_Exception(String message, java.lang.Throwable cause) {
super(message);
this.YourCustomException = new YourCustomException();
this.YourCustomException.setExName(cause.getClass().getName());
this.YourCustomException.setExMessage(cause.getMessage());
this.YourCustomException.setExDetails(cause.getLocalizedMessage());
}
}
Java中ComplexType的示例:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "YourCustomException ", propOrder = {
"exName",
"exMessage",
"exDetails"
})
public class YourCustomException {
protected String exName;
protected String exMessage;
protected String exDetails;
public String getExName() {
return exName;
}
public void setExName(String exName) {
this.exName = exName;
}
public String getExMessage() {
return exMessage;
}
public void setExMessage(String exMessage) {
this.exMessage = exMessage;
}
public String getExDetails() {
return exDetails;
}
public void setExDetails(String exDetails) {
this.exDetails = exDetails;
}
}
响应中的SOAP错误,强制调用方法中的IndexOutOfBounds:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<soap:Fault>
<faultcode>soap:Server</faultcode>
<faultstring>Error in your method</faultstring>
<detail>
<ns2:YourCustomException xmlns:ns2="http://serviceendpoint/">
<exName>java.lang.IndexOutOfBoundsException</exName>
<exMessage>Index: 0, Size: 0</exMessage>
<exDetails>Index: 0, Size: 0</exDetails>
</ns2:YourCustomException >
</detail>
</soap:Fault>
</soap:Body>
</soap:Envelope>
希望对你有帮助。