![](/img/trans.png)
[英]JAX-WS SOAP Faults - parse the details of the error in a SOAPFaultException
[英]JAX-WS Soap Faults not appearing in WSDL
我正在使用JAX-WS創建一個Web服務(我使用Java到WSDL方法創建它)。
我無法按照我的要求讓我的例外工作。
我創建了以下異常類:
@WebFault
public class MyWebServiceException extends SOAPFaultException {
private static final long serialVersionUID = 8234753208722614057L;
protected MyWebServiceException(SOAPFault fault) {
super(fault);
}
protected MyWebServiceException(SOAPFault fault, Throwable throwable) {
this(fault);
super.setStackTrace(throwable.getStackTrace());
}
}
這允許我在SOAP響應中包含以下內容:
<faultcode>E0010</faultcode>
<faultstring>Invalid Report</faultstring>
<detail>
<ns2:exception class="com.example.web.common.exceptions.MyWebServiceException" note="To disable this feature, set com.sun.xml.ws.fault.SOAPFaultBuilder.disableCaptureStackTrace system property to false" xmlns:ns2="http://jax-ws.dev.java.net/">
<message>Invalid Report</message>
<ns2:stackTrace>
<ns2:frame class="com.example.web.common.exceptions.ExceptionHandler" file="ExceptionHandler.java" line="34" method="getWebException"/>
但是,因為我的異常是擴展RuntimeException
的SOAPFaultException
,所以它沒有被添加到WSDL中,因此當服務的用戶生成其客戶端存根時,不包括異常。
有誰知道如何創建此異常以給我預期的輸出(即包括faultcode和faultstring),但也出現在WSDL中(我想它需要是一個經過檢查的異常)
我搜索了高低,想出了我已經擁有的東西!
如果您聲明WebMethod拋出MyWebServiceException,它將出現在WSDL上。
但是你可能不應該使用SOAPFault代碼。 如果您創建了一個業務異常並將其拋出,您可以在客戶端獲取錯誤代碼,並與soap內部進行混亂。
你的網絡方法看起來像:
@WebMethod
public String sayHello(@WebParam String name, @WebParam String thing) throws MyException
{
// TODO Auto-generated method stub
if ("err".equals(name))
throw new MyException("E0010","Report not found");
return null;
}
您的業務例外:
public class MyException extends Exception {
private static final long serialVersionUID = -923394585528818428L;
public MyException(String errorCode,String errorMessage)
{
this.errorCode = errorCode;
this.errorMessage = errorMessage;
}
public String getErrorCode() {
return errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
String errorCode;
String errorMessage;
}
返回的onexception將是:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<soap:Fault>
<faultcode>soap:Server</faultcode>
<faultstring>Fault occurred while processing.</faultstring>
<detail>
<ns1:MyException xmlns:ns1="http://ws/">
<errorMessage xmlns:ns2="http://ws/">Report not found</errorMessage>
<errorCode xmlns:ns2="http://ws/">E0010</errorCode>
</ns1:MyException>
</detail>
</soap:Fault>
</soap:Body>
</soap:Envelope>
您還可以添加堆棧跟蹤,但最好是登錄服務器,如果需要,可以使用GUUID與客戶端錯誤相關聯。
假設您使用的是JAX-WS而不是JAX-RPC,則可以刪除extends SOAPFaultException
。 然后將必填字段添加到您自己的異常中:
這是一個StackTraceElement(僅作為示例):
public class SOAPStackElement implements Serializable {
private static final long serialVersionUID = 1L;
@XmlAttribute(name="class")
private String class;
@XmlAttribute(name="file")
private String file;
@XmlAttribute(name="methodname")
private String methodName;
@XmlAttribute(name="line")
private Integer line;
}
這是SOAP-Info:
public class SOAPFaultInfo implements Serializable {
@XmlElementWrapper(name="stackTrace")
private SOAPStackElement[] stackTrace;
@XmlElement(name="faultcode")
private String faultCode;
@XmlElement(name="faultstring")
private String faultString;
/* what else your heart desires. :-) */
}
這是一個真正的例外:
@WebFault
public class MyWebServiceException extends Exception {
private static final long serialVersionUID = 1L;
private SOAPFaultInfo faultInfo;
protected MyWebServiceException(SOAPFaultInfo fault) {
super();
this.faultInfo = fault;
}
protected MyWebServiceException(SOAPFaultInfo fault, Throwable cause) {
super(cause);
this.faultInfo = fault;
}
public SOAPFaultInfo getFaultInfo() {
return faultInfo;
}
}
老實說,我沒有把它變成一個測試項目。 所以你可能需要解決一些編譯問題。 但我希望你能得到這張照片。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.