我有一個問題。我已經從 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>
“故障字串”標簽中的文本是我在例外中拋出的訊息。現在我的目標是在回應中發送此類資訊:
- 關于例外的可讀訊息
- 服務器拋出的例外
- 最終有關例外的詳細資訊
我在 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>
問題是,如您所見,在我的代碼中,它沒有顯示“詳細資訊”標簽。你們知道如何在 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 上。
uj5u.com熱心網友回復:
對于仍在尋找答案的每個人,我都明白,基本上,如果您有 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錯誤填充了“詳細資訊”標簽。
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>
希望這可以幫助。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/490764.html