Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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? 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.

like image 415
Arci Avatar asked Nov 28 '12 01:11

Arci


People also ask

How do you add SOAP fault to WSDL?

Using @WebFault or defining a <soap:fault> element in the WSDL file is used to declare that a specific operation might return a custom SOAP fault. @WebFault will definitely add a <soap:fault> element in the resulting WSDL. To recap, throwing an Exception will insert a element in the soap response.

How do you throw a soapFault in Java?

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 .

How are errors in SOAP messages sent?

The SOAP <Fault> element is used to transmit error and status information within a SOAP message. The <Fault> element is a child of the body element. There can be only one <Fault> element in the body of a SOAP message.

What is a SOAP exception error?

A SOAP fault is an error in a SOAP (Simple Object Access Protocol) communication resulting from incorrect message format, header-processing problems, or incompatibility between applications.


1 Answers

Use the @WebFault annotation.

You can see a good example in Using SOAP Faults and Exceptions in Java JAX-WS Web Services - 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

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);     }  } 

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> 
like image 56
Paul Vargas Avatar answered Sep 28 '22 04:09

Paul Vargas