[英]How to throw a custom fault on a JAX-WS web service?
How do you throw a custom soap fault on a JAX-WS web service? 如何在JAX-WS Web服务上抛出自定义soap错误? How can I specify the
faultCode
, faultString
and detail
of the soap fault? 如何指定
faultCode
, faultString
和soap fault的detail
? Is it possible to set the value of the detail
as bean instead of a String
? 是否可以将
detail
的值设置为bean而不是String
?
Please note that I'm developing using code-first approach. 请注意,我正在使用代码优先方法开发。
Use the @WebFault
annotation. 使用
@WebFault
批注。
You can see a good example in Using SOAP Faults and Exceptions in Java JAX-WS Web Services - Eben Hewitt on Java . 您可以在Java JAX-WS Web服务中使用SOAP故障和异常--Eben Hewitt on Java中看到一个很好的示例。
You will see the example: 你会看到这个例子:
@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;
}
}
UPDATE UPDATE
Another way is to declare the typical exception in the throws
clause. 另一种方法是在
throws
子句中声明典型的异常 。
eg Suppose the following is my exception class: 例如假设以下是我的异常类:
package pkg.ex;
public class FooException extends Exception {
public FooException(String message, Throwable cause) {
super(message, cause);
}
}
And the next class is the service implementation. 下一课是服务实现。
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;
}
}
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>
There is no problem: 没有问题:
<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>
But... 但...
<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>
Then... 然后...
<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>
import javax.xml.soap.SOAPFactory;
import javax.xml.soap.SOAPFault;
import javax.xml.ws.soap.SOAPFaultException;
import javax.xml.namespace.QName;
...
SOAPFactory soapFactory = SOAPFactory.newInstance();
SOAPFault soapFault = soapFactory.createFault(
"Your custom message",
new QName("http://schemas.xmlsoap.org/soap/envelope/", "Client"));
throw new SOAPFaultException(soapFault);
To choose the right fault code, see http://www.tutorialspoint.com/soap/soap_fault.htm . 要选择正确的故障代码,请访问http://www.tutorialspoint.com/soap/soap_fault.htm 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.