[英]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.