SOAP Fault Example
The example SOAPFaultTest.java, based on the code fragments in the sections Creating and Populating a SOAPFault Object andRetrieving Fault Information, creates a message that has a SOAPFault object. It then retrieves the contents of the SOAPFault object and prints them. You will find the code for SOAPFaultTest in the following directory:
tut-install/javaeetutorial5/examples/saaj/fault/src/
Like HeaderExample, the SOAPFaultTest class contains code that allows you to generate either a SOAP 1.1 or a SOAP 1.2 message.
Building and Running the SOAP Fault Example
To build the program using NetBeans IDE, follow these steps:
-
In NetBeans IDE, choose Open Project from the File menu.
-
In the Open Project dialog, navigate to tut-install/javaeetutorial5/examples/saaj/.
-
Select the fault folder.
-
Select the Open as Main Project check box.
-
Click Open Project.
A Reference Problems dialog appears. Click Close.
-
Right-click the fault project and choose Resolve Reference Problems.
-
In the Resolve Reference Problems dialog, select the first of the missing JAR files and click Resolve.
The missing files are activation.jar, javaee.jar, and appserv-ws.jar.
-
Navigate to the as-install/lib/ directory.
-
Select the missing JAR file (activation.jar, for example) and click Open.
In the Resolve Reference Problems dialog, all the files have green check marks to the left of their names.
-
Click Close.
-
Right-click the project and choose Build.
To run the program using NetBeans IDE, follow these steps:
-
Right-click the fault project and choose Properties.
-
Select Run from the Categories tree.
-
In the Arguments field, type the following:
1.1
This argument specifies the version of SOAP to be used in generating the message.
-
Click OK.
-
Right-click the project and choose Run.
-
Right-click the project and choose Properties.
-
Select Run from the Categories tree.
-
In the Arguments field, type the following:
1.2
-
Click OK.
-
Right-click the project and choose Run.
To build and run SOAPFaultTest using Ant, go to the directory tut-install/javaeetutorial5/examples/saaj/fault/. Use one of the following commands:
ant run-fault -Dsoap=1.1 |
ant run-fault -Dsoap=1.2 |
When you run SOAPFaultTest to generate a SOAP 1.1 message, you will see output like the following (line breaks have been inserted in the message for readability):
Here is what the XML message looks like: <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-ENV:Header/><SOAP-ENV:Body> <SOAP-ENV:Fault><faultcode>SOAP-ENV:Client</faultcode> <faultstring>Message does not have necessary info</faultstring> <faultactor>http://gizmos.com/order</faultactor> <detail> <PO:order xmlns:PO="http://gizmos.com/orders/"> Quantity element does not have a value</PO:order> <PO:confirmation xmlns:PO="http://gizmos.com/confirm"> Incomplete address: no zip code</PO:confirmation> </detail></SOAP-ENV:Fault> </SOAP-ENV:Body></SOAP-ENV:Envelope> SOAP fault contains: Fault code = {http://schemas.xmlsoap.org/soap/envelope/}Client Local name = Client Namespace prefix = SOAP-ENV, bound to http://schemas.xmlsoap.org/soap/envelope/ Fault string = Message does not have necessary info Fault actor = http://gizmos.com/order Detail entry = Quantity element does not have a value Detail entry = Incomplete address: no zip code |
When you run SOAPFaultTest to generate a SOAP 1.2 message, the output looks like this:
Here is what the XML message looks like: <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"> <env:Header/><env:Body> <env:Fault> <env:Code><env:Value>env:Sender</env:Value></env:Code> <env:Reason><env:Text xml:lang="en-US"> Message does not have necessary info </env:Text></env:Reason> <env:Role>http://gizmos.com/order</env:Role> <env:Detail> <PO:order xmlns:PO="http://gizmos.com/orders/"> Quantity element does not have a value</PO:order> <PO:confirmation xmlns:PO="http://gizmos.com/confirm"> Incomplete address: no zip code</PO:confirmation> </env:Detail></env:Fault> </env:Body></env:Envelope> SOAP fault contains: Fault code = {http://www.w3.org/2003/05/soap-envelope}Sender Local name = Sender Namespace prefix = env, bound to http://www.w3.org/2003/05/soap-envelope Fault reason text = Message does not have necessary info Fault role = http://gizmos.com/order Detail entry = Quantity element does not have a value Detail entry = Incomplete address: no zip code |