Bagaimana Anda melempar kesalahan sabun khusus pada layanan web JAX-WS? Bagaimana saya bisa menentukan faultCode
, faultString
dan detail
dari soap fault? Apakah mungkin untuk mengatur nilai dari detail
sebagai bean, bukannya String
?
Harap dicatat bahwa saya sedang mengembangkan menggunakan pendekatan code-first.
Gunakan anotasi @WebFault
.
Anda dapat melihat contoh yang bagus di Using SOAP Faults and Exceptions in Java JAX-WS Web Services - Eben Hewitt on Java.
Anda akan melihat contohnya:
@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
Cara lain adalah dengan mendeklarasikan *pengecualian khas di klausa throws
.
Contoh: Misalkan berikut ini adalah kelas pengecualian saya:
package pkg.ex;
public class FooException extends Exception {
public FooException(String message, Throwable cause) {
super(message, cause);
}
}
Dan kelas berikutnya adalah implementasi layanan.
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;
}
}
Jika permintaan saya adalah:
<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>
Tidak ada masalah:
<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>
Tapi...
<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>
Kemudian...
<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);
Untuk memilih kode kesalahan yang tepat, lihat http://www.tutorialspoint.com/soap/soap_fault.htm .