Hi,

Jay SenSharma

Jay SenSharma

You can refer to the Following post if you want to Handle the SOAPFault inside your JAXRPC Style of WebService…..http://middlewaremagic.com/weblogic/?p=721 (JAXRPC SOAPFault Handling)

The following post is only intended for JAXWS Style of WebServices…

—-

Here is a Very Simple demonstration on How we can throw our Own Custom SOAPfault Exception or any Application Specific Exception. In the following demonstration we are going to throw SOAPFaultexception whenever the user Enters an Integer Input Less than 1000.

Step1). Write the WebService Class As Fllowing: “JAXWS_SOAPFault_Demo.java”

package examples.webservices.handler;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.HandlerChain;
import javax.annotation.Resource;
import javax.xml.ws.*;
import javax.xml.soap .*;
import javax.xml.namespace .*;
import javax.xml.ws.soap.SOAPFaultException;

@WebService
public class JAXWS_SOAPFault_Demo
{
@Resource
WebServiceContext ctx;

@WebMethod()
public String validateNumber(int number) throws Exception
{
System.out.println("nnt Method Invoked....String validateNumber("+number+")");
if(number<1000)
{
SOAPFactory fac = SOAPFactory.newInstance();
SOAPFault sf = fac.createFault("Sorry Number is Less Than 1000", new QName("http://schemas.xmlsoap.org/soap/envelope/", "Client"));
throw new SOAPFaultException(sf);
}
else
return "WebService Replied Correct Input: "+number;
}
}

Step2). Write the Ant Build File as follwoign to Build and Deploy the above WebService: “build.xml”

<project name="webservices-hello_world" default="all">
<property name="wls.username" value="weblogic" />
<property name="wls.password" value="weblogic" />
<property name="wls.hostname" value="localhost" />
<property name="wls.port" value="7001" />
<property name="wls.server.name" value="AdminServer" />
<property name="ear.deployed.name" value="JAXWSFaultDemoEar" />
<property name="example-output" value="output" />
<property name="ear-dir" value="${example-output}/JAXWSFaultDemoEar" />
<property name="clientclass-dir" value="${example-output}/clientclasses" />

<path id="client.class.path">
<pathelement path="${clientclass-dir}"/>
<pathelement path="${java.class.path}"/>
</path>

<taskdef name="jwsc"
classname="weblogic.wsee.tools.anttasks.JwscTask" />

<taskdef name="wldeploy"
classname="weblogic.ant.taskdefs.management.WLDeploy"/>

<target name="all" depends="clean,build-service,deploy" />

<target name="clean" depends="undeploy">
<delete dir="${example-output}"/>
</target>

<target name="build-service">
<jwsc
srcdir="src"
destdir="${ear-dir}">
<jws file="JAXWS_SOAPFault_Demo.java" type="JAXWS"/>
</jwsc>
</target>

<target name="deploy">
<wldeploy action="deploy" name="${ear.deployed.name}"
source="${ear-dir}" user="${wls.username}"
password="${wls.password}" verbose="true"
adminurl="t3://${wls.hostname}:${wls.port}"
targets="${wls.server.name}" />
</target>

<target name="undeploy">
<wldeploy action="undeploy" name="${ear.deployed.name}"
failonerror="false"
user="${wls.username}" password="${wls.password}" verbose="true"
adminurl="t3://${wls.hostname}:${wls.port}"
targets="${wls.server.name}" />
</target>
</project>

Step3). Now create a Folder with name “src” beside “build.xml” and paste theΒ  WebService source code () inside “src” directory.

Step4). run the setDomainEnv.sh and then run the ANT.

Step5). While testing the WebService please enter inputs less than 1000 and then greater that 1000….As soon as you enter any input Less than 1000 you will see the following SOAPFault as result:

<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:Client</faultcode>
<faultstring>Sorry Number is Less Than 1000</faultstring>
<detail>
<ns2:exception xmlns:ns2="http://jax-ws.dev.java.net/" class="javax.xml.ws.soap.SOAPFaultException" note="To disable this feature, set com.sun.xml.ws.fault.SOAPFaultBuilder.disableCaptureStackTrace system property to false">
<message>Sorry Number is Less Than 1000</message>
<ns2:stackTrace>
<ns2:frame line="unknown" method="validateNumber" />
<ns2:frame class="sun.reflect.NativeMethodAccessorImpl" file="NativeMethodAccessorImpl.java" line="native" method="invoke0" />
<ns2:frame class="sun.reflect.NativeMethodAccessorImpl" file="NativeMethodAccessorImpl.java" line="39" method="invoke" />
<ns2:frame class="sun.reflect.DelegatingMethodAccessorImpl" file="DelegatingMethodAccessorImpl.java" line="25" method="invoke" />
<ns2:frame file="Method.java" line="597" method="invoke" />
<ns2:frame class="weblogic.wsee.jaxws.WLSInstanceResolver$WLSInvoker" file="WLSInstanceResolver.java" line="89" method="invoke" />
<ns2:frame class="weblogic.wsee.jaxws.WLSInstanceResolver$WLSInvoker" file="WLSInstanceResolver.java" line="71" method="invoke" />
<ns2:frame file="InvokerTube.java" line="146" method="invoke" />
<ns2:frame class="com.sun.xml.ws.server.sei.EndpointMethodHandler" file="EndpointMethodHandler.java" line="257" method="invoke" />
<ns2:frame class="com.sun.xml.ws.server.sei.SEIInvokerTube" file="SEIInvokerTube.java" line="93" method="processRequest" />
<ns2:frame file="Fiber.java" line="598" method="__doRun" />
<ns2:frame file="Fiber.java" line="557" method="_doRun" />
<ns2:frame file="Fiber.java" line="542" method="doRun" />
<ns2:frame file="Fiber.java" line="439" method="runSync" />
<ns2:frame class="com.sun.xml.ws.server.WSEndpointImpl$2" file="WSEndpointImpl.java" line="243" method="process" />
<ns2:frame class="com.sun.xml.ws.transport.http.HttpAdapter$HttpToolkit" file="HttpAdapter.java" line="444" method="handle" />
<ns2:frame class="com.sun.xml.ws.transport.http.HttpAdapter" file="HttpAdapter.java" line="244" method="handle" />
<ns2:frame class="com.sun.xml.ws.transport.http.servlet.ServletAdapter" file="ServletAdapter.java" line="134" method="handle" />
<ns2:frame class="weblogic.wsee.jaxws.HttpServletAdapter$AuthorizedInvoke" file="HttpServletAdapter.java" line="272" method="run" />
<ns2:frame class="weblogic.wsee.jaxws.HttpServletAdapter" file="HttpServletAdapter.java" line="185" method="post" />
<ns2:frame file="JAXWSServlet.java" line="180" method="doPost" />
<ns2:frame file="HttpServlet.java" line="727" method="service" />
<ns2:frame file="JAXWSServlet.java" line="64" method="service" />
<ns2:frame file="HttpServlet.java" line="820" method="service" />
<ns2:frame class="weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction" file="StubSecurityHelper.java" line="227" method="run" />
<ns2:frame class="weblogic.servlet.internal.StubSecurityHelper" file="StubSecurityHelper.java" line="125" method="invokeServlet" />
<ns2:frame class="weblogic.servlet.internal.ServletStubImpl" file="ServletStubImpl.java" line="292" method="execute" />
<ns2:frame class="weblogic.servlet.internal.ServletStubImpl" file="ServletStubImpl.java" line="175" method="execute" />
<ns2:frame class="weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction" file="WebAppServletContext.java" line="3498" method="run" />
<ns2:frame class="weblogic.security.acl.internal.AuthenticatedSubject" file="AuthenticatedSubject.java" line="321" method="doAs" />
<ns2:frame line="unknown" method="runAs" />
<ns2:frame class="weblogic.servlet.internal.WebAppServletContext" file="WebAppServletContext.java" line="2180" method="securedExecute" />
<ns2:frame class="weblogic.servlet.internal.WebAppServletContext" file="WebAppServletContext.java" line="2086" method="execute" />
<ns2:frame class="weblogic.servlet.internal.ServletRequestImpl" file="ServletRequestImpl.java" line="1406" method="run" />
<ns2:frame file="ExecuteThread.java" line="201" method="execute" />
<ns2:frame file="ExecuteThread.java" line="173" method="run" />
</ns2:stackTrace>
</ns2:exception>
</detail>
</S:Fault>
</S:Body>
</S:Envelope>

.

.

Thanks

Jay SenSharma

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.