Hello World with JAX-WS

Create Eclipse project
-Start Eclipse
-Create a new Java Project named helloworld.
-Add all jars in jaxws-ri (ref implementation) lib directory to project Libraries.

Server Implementation
-Create a new java package named hello.server
-Create a new class named HelloworldImpl. Annotate class with @WebService
-Create a new method String sayHello(String hello)
-Create a new class name HelloworldException extends Exception

package hello.server;
 
import javax.jws.WebService;
 
@WebService
public class HelloworldImpl {
 
  public String sayHello(String hello) 
    throws HelloworldException{
    System.out.printf("Prepare to say: %s\n", hello);
    if (hello.trim().length() < 1)
      throw new HelloworldException(
        "Hello World Error.", 
        "You need to provide a string");
    return String.format("Hello, %s, from jax-ws!\n", hello);
  }
}

Build Server with Ant
-Setup environment variables
JAVA_HOME
CATALINA_HOME
JAXWS_HOME
-Create a new Ant build file named build.xml
-Setup jaxws classpath

<path id="jaxws.classpath">
  <pathelement location="${java.home}/../lib/tools.jar"/>
  <fileset dir="${lib.home}">
    <include name="*.jar"/>
    <exclude name="j2ee.jar"/>
  </fileset>
</path>

-Import ant tasks

<taskdef name="apt" 
  classname="com.sun.tools.ws.ant.Apt">
  <classpath refid="jaxws.classpath"/>
</taskdef>
 
<taskdef name="wsimport" 
  classname="com.sun.tools.ws.ant.WsImport">
  <classpath refid="jaxws.classpath"/>
</taskdef>

Generate server side java code

<target name="generate-server" depends="setup">
  <apt
    fork="true"
    debug="true"
    verbose="true"
    destdir="${build.classes.home}"
    sourcedestdir="${basedir}/src"
    sourcepath="${basedir}/src">
    <classpath>
      <path refid="jaxws.classpath"/>
      <pathelement location="${basedir}/src"/>
    </classpath>
    <option key="r" value="${build.home}"/>
    <source dir="${basedir}/src">
      <include name="**/server/*.java"/>
    </source>
  </apt>
</target>

-This generates three java sources in the hello.server.jaxws pacakge
SayHello.java
SayHelloResponse.java
HelloworldExceptionBean.java

Create war file

<target name="create-war">
  <war warfile="${build.war.home}/jaxws-${ant.project.name}.war" 
    webxml="web.xml">
    <webinf dir="${basedir}" 
      includes="sun-jaxws.xml"/>
    <classes dir="${build.classes.home}"/>
  </war>
</target>

-web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
  xmlns="http://java.sun.com/xml/ns/j2ee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
  http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <description>hello world from jaxws</description>
  <display-name>hello world</display-name>
  <listener>
    <listener-class>com.sun.xml.ws.transport.http.servlet
    .WSServletContextListener</listener-class>
  </listener>
  <servlet>
    <description>Hello world from jaxws</description>
    <display-name>helloworld</display-name>
    <servlet-name>helloworld</servlet-name>
    <servlet-class>com.sun.xml.ws.transport.http.servlet
    .WSServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>helloworld</servlet-name>
    <url-pattern>/sayHello</url-pattern>
  </servlet-mapping>
  <session-config>
    <session-timeout>60</session-timeout>
  </session-config>
</web-app>

-sun-jaxws.xml

<?xml version="1.0" encoding="UTF-8"?>
<endpoints 
  xmlns='http://java.sun.com/xml/ns/jax-ws/ri/runtime' 
  version='2.0'>
  <endpoint
    name='helloworld'
    implementation='hello.server.HelloworldImpl'
    url-pattern='/sayHello'/>
</endpoints>

Deploy war to Tomcat 5.x

<target name="deploy-tomcat">
  <copy 
    file="${build.war.home}/jaxws-${ant.project.name}.war"
    todir="${env.CATALINA_HOME}/webapps"
    overwrite="true"/>
</target>

Test Hello World Web Service
-Might need to restart Tomcat
-Point browser to http://localhost:8080/jaxws-helloworld/sayHello for web service summary page
-Point browser to http://localhost:8080/jaxws-helloworld/sayHello?wsdl for wsdl page

Client Implementation
-Create a new package named hello.client
-Generate Client java codes from wsdl with Ant tool

<target name="generate-client" depends="setup">
  <wsimport
    debug="true"
    verbose="true"
    keep="true"
    destdir="${build.classes.home}"
    sourcedestdir="${basedir}/src"
      package="hello.client"
      wsdl="http://localhost:8080/jaxws-helloworld/sayHello?wsdl">
  </wsimport>
</target>

This generates seven java files from wsdl
HelloworldException.java
HelloworldException_Exception.java
HelloworldImpl.java
HelloworldImplService.java
ObjectFactory.java
SayHello.java
SayHelloResponse.java

-Create a new class name HelloworldClient.java

package hello.client;
 
public class HelloworldClient {
 
  public static void main(String[] args){
    try{
      HelloworldImpl port = new HelloworldImplService()
        .getHelloworldImplPort();
      String helloStr = "JAX-WS";
      sayHello(port, helloStr);
      helloStr = "";
      sayHello(port, helloStr);
    }catch(HelloworldException_Exception e){
      System.out.printf("HelloworldException_Exception: %s", 
          e.getFaultInfo().getDetail());
    }
  }
 
  private static void sayHello(HelloworldImpl port, String helloStr) 
    throws HelloworldException_Exception {
    String hello = port.sayHello(helloStr);
    System.out.printf("Say hello to %s returns: %s", helloStr, hello);
  }
}

-Run HelloworldClient give:
Say hello to JAX-WS returns: Hello, JAX-WS, from jax-ws!
HelloworldException_Exception: You need to provide a string

Source Code

This entry was posted in java, jax-ws, soa. Bookmark the permalink.