如何在JAX-WS Web服务上引发自定义错误? [英] How to throw a custom fault on a JAX-WS web service?
问题描述
如何在JAX-WS Web服务上引发自定义的肥皂错误?如何指定 faultCode
, faultString
和 detail
肥皂故障?可以将 detail
的值设置为bean而不是 String
?
How do you throw a custom soap fault on a JAX-WS web service? How can I specify the faultCode
, faultString
and detail
of the soap fault? Is it possible to set the value of the detail
as bean instead of a String
?
请注意,我正在开发使用代码优先的方法。
Please note that I'm developing using code-first approach.
推荐答案
使用 @WebFault
注释。
您可以在在Java中使用SOAP故障和异常JAX-WS Web服务 - Eben Hewitt on Java < a>。
You can see a good example in Using SOAP Faults and Exceptions in Java JAX-WS Web Services - Eben Hewitt on Java.
您将看到示例:
@WebFault(name="CheckVerifyFault",
targetNamespace="http://www.example.com")
public class CheckVerifyFault extends Exception {
/**
* Java type that goes as soapenv:Fault detail element.
*/
private CheckFaultBean faultInfo;
public CheckVerifyFault(String message, CheckFaultBean faultInfo) {
super(message);
this.faultInfo = faultInfo;
}
public CheckVerifyFault(String message, CheckFaultBean faultInfo,
Throwable cause) {
super(message, cause);
this.faultInfo = faultInfo;
}
public CheckFaultBean getFaultInfo() {
return faultInfo;
}
}
更新
另一种方法是在<$ c中声明 典型异常 $ c> throws 子句。
Another way is to declare the typical exception in the throws
clause.
例如假设以下是我的例外类:
e.g. Suppose the following is my exception class:
package pkg.ex;
public class FooException extends Exception {
public FooException(String message, Throwable cause) {
super(message, cause);
}
}
下一课是服务
package pkg.ws;
import javax.jws.WebService;
import pkg.ex.FooException;
@WebService(serviceName = "FooSvc")
public class FooService {
public String sayHello(String name) throws FooException {
if (name.isEmpty()) {
Throwable t = new IllegalArgumentException("Empty name");
throw new FooException("There is one error", t);
}
return "Hello, " + name;
}
}
如果我的请求是: p>
If my request is:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ws="http://ws.pkg/">
<soapenv:Header/>
<soapenv:Body>
<ws:sayHello>
<arg0>Peter</arg0>
</ws:sayHello>
</soapenv:Body>
</soapenv:Envelope>
没有问题:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:sayHelloResponse xmlns:ns2="http://ws.pkg/">
<return>Hello, Peter</return>
</ns2:sayHelloResponse>
</S:Body>
</S:Envelope>
但是...
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ws="http://ws.pkg/">
<soapenv:Header/>
<soapenv:Body>
<ws:sayHello>
<arg0></arg0>
</ws:sayHello>
</soapenv:Body>
</soapenv:Envelope>
然后...
<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>There is one error</faultstring>
<detail>
<ns2:FooException xmlns:ns2="http://ws.pkg/">
<message>There is one error</message>
</ns2:FooException>
</detail>
</S:Fault>
</S:Body>
</S:Envelope>
这篇关于如何在JAX-WS Web服务上引发自定义错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!