Hi,

Jay SenSharma

Jay SenSharma

Here is a Simple demonstration of creating Custom SOAPFault messages in case of WebLogic JAXRPC webservices. This testCase will generate the custom SOAPFault messahe whenever a user will enter a negative value to add the numbers.

Step1). Develope the “AddNumbersException.java” Custom Exception class.

package annotations.server;
 public class AddNumbersException extends Exception 
  {
    String info;
    public AddNumbersException(String message, String detail) 
     {
       super(message);
       this.info = detail;
     }

   public String getFaultInfo() 
   {
      return info;
   }
  }

Step2). Now develope the WebService Class “AddNumbersImpl.java

package annotations.server;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.soap.SOAPBinding;
import javax.jws.WebResult;
@WebService
@SOAPBinding(style=SOAPBinding.Style.RPC, use=SOAPBinding.Use.LITERAL)
public class AddNumbersImpl {

@WebResult(name="return")
public int addNumbers(int number1,int number2) throws AddNumbersException {
if (number1 < 0 || number2 < 0) {
throw new AddNumbersException("Negative number cant be added!","Numbers: " + number1 + ", " + number2);
}
return number1 + number2;
}
}

Step3). Now develope the ANT build file “build.xml” in the same directory whereever you have placed the “AddNumbersImpl.java” and “AddNumbersException.java” Sources.

<project name="webservices-security_jws" default="all" basedir=".">
<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="ws.file" value="AddNumbersImpl" />

<property name="example-output" value="output" />
<property name="ear-dir" value="${example-output}/AddNumbersEar" />
<property name="clientclass-dir" value="${example-output}/clientclasses"/>
<property name="ear.deployed.name" value="AddNumbersImplEar"/>

<property name="base.dir" value="." />
<property name="ear.dir" value="${example-output}/AddNumbersImplEar" />
<property name="client.dir" value="${example-output}/AddNumbers_client" />

<path id="client.class.path">
<pathelement path="${client.dir}"/>
<pathelement path="${java.class.path}"/>
<pathelement path="${ear.dir}/APP-INF/classes" />
</path>

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

<target name="all" depends="clean, server, client, deploy" />
<target name="build" depends="clean, server, client" />

<target name="clean">
<delete dir="${ear.dir}"/>
<delete dir="${client.dir}"/>
</target>

<target name="server">
<mkdir dir="${ear.dir}"/>
<mkdir dir="${ear.dir}/APP-INF"/>
<mkdir dir="${ear.dir}/APP-INF/classes"/>
<javac srcdir="${basedir}" destdir="${ear.dir}/APP-INF/classes" classpath="${java.class.path}"
       includes="*.java"/>
<jwsc srcdir="${basedir}" destdir="${ear.dir}" classpathref="client.class.path" >
     <jws file="${ws.file}.java" explode="true"/>
</jwsc>
</target>

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

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

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

<target name="client">
<mkdir dir="${client.dir}"/>
<clientgen wsdl="${ear.dir}/${ws.file}/WEB-INF/AddNumbersImplService.wsdl" destDir="${client.dir}"
           classpath="${java.class.path}" packageName="annotations.server.client"/>
<javac srcdir="${client.dir}" destdir="${client.dir}" classpath="${java.class.path}" includes="annotations/server/client/**/*.java"/>
<javac srcdir="${basedir}" destdir="${client.dir}" classpath="${java.class.path};${client.dir}" includes="AddNumbersImplClient.java"/>
</target>
</project>

Step4). Now Just run the “setDomainEnv.sh” then run the ANT build file…

Open Admin Console and use the testClient Page to test your WebService ….

Just Enter a Negative Integer value to add two numbers there and you will see that WebService Generates a Custom SOAPFault Message similar to :

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header />
<env:Body>
<env:Fault>
<faultcode>env:Server</faultcode>
<faultstring>Negative number cant be added!</faultstring>
<detail>
<java:AddNumbersException xmlns:java="java:annotations.server" />
</detail>
</env:Fault>
</env:Body>
</env: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.