java JAX-WS Soap 错误未出现在 WSDL 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15358204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JAX-WS Soap Faults not appearing in WSDL
提问by cowls
I am creating a web service using JAX-WS (I am creating this using the Java to WSDL approach).
我正在使用 JAX-WS 创建一个 Web 服务(我正在使用 Java 到 WSDL 方法创建它)。
Im having trouble getting my exception to work as I require.
我很难让我的例外按照我的要求工作。
I have created the following exception class:
我创建了以下异常类:
@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());
}
}
This allows me to have the following in my SOAP response:
这允许我在 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"/>
However, because my exception is a SOAPFaultException
which extends RuntimeException
, it is not being added to the WSDL, so when users of the service generate their client stubs the exception is not included.
但是,因为我的异常是一个SOAPFaultException
which extends RuntimeException
,它没有被添加到 WSDL,所以当服务的用户生成他们的客户端存根时,异常不包括在内。
Does anyone know how to create this exception to give me the intended output (ie including the faultcode and faultstring) but also appear in the WSDL (I guess it needs to be a checked exception)
有谁知道如何创建这个异常来给我预期的输出(即包括错误代码和错误字符串)但也出现在 WSDL 中(我猜它需要是一个检查异常)
I searched SO high and low to come up with what I have already!
我搜索了这么高和低,想出了我已经拥有的东西!
采纳答案by jagra
If you declare that your WebMethod throws MyWebServiceException, it will appear on WSDL.
如果您声明您的 WebMethod 抛出 MyWebServiceException,它将出现在 WSDL 上。
But probably you should not mess with SOAPFault codes. If you create a business exception and throw it, you can get error codes in client withoud messing with soap internals.
但是您可能不应该弄乱 SOAPFault 代码。如果您创建一个业务异常并抛出它,您可以在客户端中获得错误代码,而不会弄乱soap内部结构。
Your webmethod would look like:
您的网络方法如下所示:
@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;
}
Your business exception:
您的业务例外:
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;
}
And the return onexception will be:
返回的异常将是:
<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>
You can also add a stack trace but that would be better to get logged in server, maybe with a GUUID to correlate with client error if needed.
您还可以添加堆栈跟踪,但最好登录服务器,如果需要,可以使用 GUID 与客户端错误相关联。
回答by secra
Assuming You are using JAX-WS and not JAX-RPC you could just remove the extends SOAPFaultException
. Afterwards add the required fields to your own exception:
假设您使用的是 JAX-WS 而不是 JAX-RPC,您可以删除extends SOAPFaultException
. 然后将必填字段添加到您自己的异常中:
This is a StackTraceElement (just as an example):
这是一个 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;
}
This is the SOAP-Info:
这是 SOAP 信息:
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. :-) */
}
And this is the real exception:
这是真正的例外:
@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;
}
}
I honestly didnt get around to putting this into a test project. So you might need to iron out some compile issues. But I hope you get the picture.
老实说,我没有考虑将其放入测试项目中。所以你可能需要解决一些编译问题。但我希望你能得到这张照片。