WebService by Bottom Up approach using ant script

The JAX-WS allows you to implement a simple java class as a webservice by exposing its public methods as webservice operations.

There are two programming approaches to develop a WebService.

 

1. Code-First This is a bottom-up, implementation-first strategy where we write the Java class and the data POJOs representing the complex data types used by the web service operations. Then, we use WebLogic Server’s JAX-WS tools to generate the web service’s WSDL interface and associated XML schema types. In JAX-WS terminology, this approach is often referred to as Java-to-WSDL.

 

This approach consumes a JWS (Java Web Service) file, which is a simple java class annotated with @WebService annotation at the beginning that tells the Java interpreter that you intend to publish the methods of this class as a web service.

The output is a WSDL (Web Service Descriptor Language) an XML language for describing Web services.

 

2. WSDL-First This is a top-down, interface-first strategy where we manually create the web service’s WSDL interface and any associated XML schemas. Then, we use WebLogic Server’s JAX-WS tools to generate the Java interface and the data POJOs. Finally, we complete the web service implementation by writing a class that implements the generated Java interface. In JAX-WS terminology, this approach is often referred to as WSDL-to-Java.

The below post shows a sample example of developing a WebService by BottomUp approach using an ant build script.

1: Generate a JWS file.

JWS file can be developed by annotating a simple java class file using the @webservice annotation.

A sample JWS file looks like below.

****************************************************************

 

package com.test.webservice;
import javax.jws.WebService;

@WebService(name="HelloWorldPortType", serviceName="HelloWorldService")
public class HelloWorldImpl {
public String sayHelloWorld(String message) {
try {
System.out.println("sayHelloWorld:" + message);
}
catch (Exception ex) { ex.printStackTrace(); }
return "Here is the message: '" + message + "'";
}
}

 

****************************************************************

2: Compile the JWS file.

Save the file to any folder like c:HelloWorldWebService and compile the same as below.

javac –d . HelloWorldImpl.java

That would create the directory structure according to package name specified in the class.

3. Create a standard Ant build script:

In the ant script, include the WebLogic’s jwsc ant task.

The jwsc Ant task takes as input one or more Java Web Service (JWS) files that contains both standard (JSR-181) and WebLogic-specific JWS annotations and generates all the artifacts you need to create a WebLogic Web Service.

A jwsc ant task definition looks like below.

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

A sample jwsc ant task looks like below.

 

<target name="build-service">

<jwsc

srcdir="."

destdir="${ear-dir}">

<jws file="examples/webservices/hello_world/HelloWorldImpl.java"

type="JAXWS"/>

</jwsc>

</target>

 

NOTE: After generating all the artifacts, the jwsc Ant task compiles the Java and JWS files, packages the compiled classes and generated artifacts into a deployable Web application WAR file, and finally creates an exploded Enterprise Application directory that contains the JAR file. You then deploy this Enterprise Application to WebLogic Server.

4: Generating the client specific files (Optional):

If you want to generate components that client can use to invoke the WebServices, then include the clientgen ant task. This ant task reads from the existing WSDL file to generate the components.

A clientgen ant task definition looks like below.

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

A sample clientgen ant task would look like below.

 

<clientgen

wsdl="http://${wls.hostname}:${wls.port}/HelloWorldImpl/HelloWorldService?WSDL"

destDir="${clientclass-dir}"

packageName="com.test.webservice.client"

type="JAXWS"/>

 

Along with the above annotations, we specify other generic tasks like wldeploy etc.

A complete ant build script would look like below.

*************************************************************

 

<project name="webservices-hello_world" default="all">

<!-- set global properties for this build -->

<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="helloWorldEar" />

<property name="example-output" value="output" />

<property name="ear-dir" value="${example-output}/helloWorldEar" />

<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="clientgen"

classname="weblogic.wsee.tools.anttasks.ClientGenTask" />

<taskdef name="wldeploy"

classname="weblogic.ant.taskdefs.management.WLDeploy"/>

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

<target name="clean" depends="undeploy">

<delete dir="${example-output}"/>

</target>

<target name="build-service">

<jwsc

srcdir="."

destdir="${ear-dir}">

<jws file="HelloWorldImpl.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>

<target name="client">

<clientgen

wsdl="http://${wls.hostname}:${wls.port}/HelloWorldImpl/HelloWorldService?WSDL"

destDir="${clientclass-dir}"

packageName="com.test.webservice.client"

type="JAXWS"/>

<javac

srcdir="${clientclass-dir}" destdir="${clientclass-dir}"

includes="**/*.java"/>

<javac

srcdir="." destdir="${clientclass-dir}"

includes="com/test/webservice/client/**/*.java"/>

</target>

<target name="run">

<java classname="client.HelloWorldService"

fork="true" failonerror="true" >

<classpath refid="client.class.path"/>

<arg line="http://${wls.hostname}:${wls.port}/HelloWorldImpl/HelloWorldService"  />

</java> </target>

</project>

 

*************************************************************

5. Generate the WebServices artifacts:

Generate the WebServices artifacts by running the ant script as below:

ant build-service

This would generate the required WebServices Artifacts.

6. Deploy the WebService:

Deploy the WebService on to the WebLogic Server by running the below command.

ant deploy

7. Test the WebService:

Test that the Web Service is deployed correctly by invoking its WSDL in your browser:

http://host:port/HelloWorldImpl/HelloWorldService?WSDL

A sample WSDL would look like below.

This suggests that the WebService is successfully deployed.

WebLogic server provides a Test Tool for testing the WebServices where you can see the Service request/response.

a.  Go to the WebService EAR Application deploymed –>  Testing  –>  Select the testing point as ‘Test Client

b. Pass the argument and click on the WebService operation.

NOTE: You can download the source files used for the demo, below:

Source

If you want to create a WebService in an IDE (Eclipse) refer the below post.

https://weblogic-wonders.com/weblogic/2011/05/19/webservices-in-weblogic/

Further reading:

http://download.oracle.com/docs/cd/E13222_01/wls/docs103/webserv/use_cases.html

Cheers,

Wonders Team. 🙂

7 comments

  1. Wow, awesome blog layout! How long have you
    been blogging for? you make blogging look easy. The overall look of your site is excellent,
    as well as the content!

    1. Thanks Charley for your feedback.

      Any suggestions are welcome.

      Cheers,
      Wonders team.

  2. Great blog…you have explained it so clearly,many of my doubts got cleared…please keep the good work and keep blogging,keep helping 🙂

  3. Very informative !!
    Could you let me know to use t3s instead of t3 to connect to admin server.

    1. How do you want to connect to ADMIN? through wlst or some client code?
      You can just change the protocol to t3s instead of t3.

Comments are closed.