Easy Java web services - JAXWS Deployments

One of the things I like about Java 6 is definitely the introduction of web services to the core of its APIs. I remember about two years ago when I was frustrated about working with flat files and wanted a way to run my client test files easily.

I work with an application that interacts with other applications owned by State agencies. If you are in the tech world, and your job is to work with any State agency, you would notice that most of the applications are written in some language that is losing its workforce(COBOL).

Some of these applications communicate with other applications by generating flat files, which get loaded by the receiving applications. Reading these flat file is definitely a pain. Luckily for me I was in an environment that allowed me to be creative and I took that opportunity to introduce web services as an alternate means of communicating with the State agencies.

This post is about deploying a simple Service Endpoint Interface(SEI) and the most popular way in doing it. Lets take a look at the Endpoint below:

package example;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

@WebService(targetNamespace="http://extendit.us")

public class Hello {

    @WebMethod

    public String hello(@WebParam(name = "name") String name) {

        return "Hello " + name + "!";

    }

}

 

The easiest way to deploy this endpoint would be by using the default servlet container that comes with the JDK.

 

package example.publish

public class HelloTest {

  public static void main(String ..args){

     Endpoint.publish("http://localhost:8888/hello",new Hello());

  }

}

 

Now this would work, but it is not performant,  it would not scale. The ideal way is to deploy your endpoint in a servlet container or application server.

Let's use jaxws reference implementation with metro for our first example. Also, we would be using tomcat for our servlet container.

The easy way is to download the metro jar files and include the webservice-rt.jar file in your "CLASS PATH". That way we do not have to run wsgen to generate the stubs.

Metro uses the sun-jaxws.xml( deployment descriptor) file to define its endpoints in a flexible manner. The file should be placed under the WEB-INF folder.

<endpoints xmlns='http://java.sun.com/xml/ns/jax-ws/ri/runtime' version='2.0'>

  <endpoint name='hello'

            implementation='example.Hello'

            url-pattern='/hello'/>

</endpoints>

 

Next, you would have to add the servlet and context listener to the web.xml.

 

<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee">

  <display-name>hello</display-name>

  <listener>

    <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>

  </listener>

  <servlet>

    <servlet-name>hello</servlet-name>

    <description>JAX-WS endpoint - hello</description>

    <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>

    <load-on-startup>1</load-on-startup>

  </servlet>

  <servlet-mapping>

    <servlet-name>hello</servlet-name>

    <url-pattern>/hello</url-pattern>

  </servlet-mapping>

  <session-config>

    <session-timeout>60</session-timeout>

  </session-config>

</web-app>

 

Your directory structure in your servlet container should look like this below

 

hello service

Copy this folder to your tomcat webapps folder and start the container.

The other two popular frameworks used in the java ecosystem are Apache Axis2 and Apache CXF. The cool thing about jax-ws is that all three framework are deploy in the same manner.

You would have to use a different deployment descriptor file and make sure the right libraries are in your "CLASS PATH".

Axis2 deployment descriptor file would look like this below:

 

 

 

<serviceGroup>
  <service name="HelloService" scope="application">
    <description>Hello Service</description>
    <messageReceivers>
      <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
          class="org.apache.axis2.jaxws.server.JAXWSMessageReceiver"/>
    </messageReceivers>
    <parameter name="ServiceClass">example.Hello</parameter>
  </service>
</serviceGroup>

 

The WEB-INF folder containing the service descriptor file would look like this WEB-INF/services/hello/META-INF/services.xml and with Apache CXF, I would suggest you take a look at the jax-ws configuration they have on their site.

I hope this helps you to see how easy it is to deploy a jax-ws service endpoint interface.