Over a million developers have joined DZone.

Working with JAX-WS Web Services: Part 1

DZone's Guide to

Working with JAX-WS Web Services: Part 1

· Integration Zone ·
Free Resource

Discover how you can get APIs and microservices to work at true enterprise scale.

There are many frameworks available in the market to develop web services based on Java platform. However, most of those frameworks follow JAX-WS specification(JSR-000224). This series will concentrate on the official JAX-WS reference implementation developed by Metro Community at Oracle (formerly known as Sun)

Getting started with a Simple Web Service Development

Software Required:

  • Java SE 1.6 and above
  • JAX-WS Reference Implementation 2.2.5 (available here)
  • Apache Tomcat  version 6 and above.

A web service can be developed in two methods,

1. Top-down approach

In this approach, the service interface is created first and the implementation is provided at later point.

2. Bottom-up approach

In this approach, the service implementation is created first, and based on that the interface is defined. This approach is easy to for beginners who are new to web services.

We follow the second approach, i.e., bottom-up approach for simplicity and to have a working example ready more quickly.

What to do?

Create a POJO(Plain old java object) class, and annotate it with WebService as mentioned below

package com.accrd.blog.ws.jaxws.sample;

import javax.jws.WebService;

public class SimpleWebService {


That makes the java class a web service with the name “SimpleWebService”. We can add the required functionality to the class, so that the service can serve some purpose.

Let’s add a method that takes a name String as input and returns a “hello” message.

package com.accrd.blog.ws.jaxws.sample;

import javax.jws.WebService;

public class SimpleWebService {

    public String sayHello (String name){
        return "Hello, "+ name + "!" ;


Now, the web service has got a task to do, and the operation available does this: you pass a name(“web service”) and it returns hello message to the given name(“Hello, web service!”).

That’s it. The web service is ready for deployment.

You can deploy this in two ways,

1. Standalone deployment: In this approach, you just need to have a main method and call the Endpoint.publish(url, provider) . This is mentioned below. This creates web service run-time environment that shipped with Java SE 6 and deploys it into light-weight http server. The service is accessible with the url provided as input to the publish method.

package com.accrd.blog.ws.jaxws.sample;

import javax.jws.WebService;
import javax.xml.ws.Endpoint;

public class SimpleWebService {

    public String sayHello (String name){
        return "Hello, "+ name + "!" ;

    public static void main (String args[]){
        Endpoint.publish("http://localhost:8080/SimpleWebService", new SimpleWebService ()); ;


2. Deployment on to a servlet-container: In this approach, we can deploy the web service by creating a standard java web application .war file and deploying that to a web server which has got a servlet container support, such as Tomcat or Jetty. This approach is explained further below using Apache Tomcat version 6.

1. Make sure, you have downloaded and unzipped the JAX-WS reference implementation(available here).

2. Make sure, you have downloaded and installed Apache Tomcat version 6.

3. Set the JAVA_HOME environment variable to point to the JDK 1.6 or above installation folder.

4. Create a standard java web application with your favorite IDE(such as Eclipse or NetBeans).

5. Create/Edit the web application deployment descriptor(web.xml) with the following content.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">




The listener class(com.sun.xml.ws.transport.http.servlet.WSServletContextListener) mentioned above is the web service context listener, which initializes the web service context when the application context is initialized and creates a web service delegate which is used to delegate all future web service requests and route them to appropriate end point implementation defined in the sun-jaxws.xml mentioned below. This stores the created delegate in the container servlet context, so that the delegate can be accessed by other servlets.


The servlet definition and it’s mapping is used to intercept the url-pattern that should be considered as web service request.

The class(com.sun.xml.ws.transport.http.servlet.WSServlet) acts as a dispatching servlet that routes the request to appropriate implementation class through the delegate received from the servlet context created by the listener as stated above.

6. Create an another required web service deployment descriptor(sun-jaxws.xml) under the WEB-INF folder. This file is needed by the JAX-WS reference implementation to map the service implementation class with the service interface. The content of this file is mentioned below.

<?xml version="1.0" encoding="UTF-8"?>

        url-pattern="/SimpleWebService" />


sun-jaxws.xml descriptor: Each endpoint definition, in this descriptor indicates the name of the web service, implementation class and the url-pattern that routes to this web service invocation. This is read by the context listener and handed-over to the web service delegate created by it, so that the delegate knows which implementation class to invoke when a web service request came-in.

7. Copy the libraries available under <JAX-WS_INSTALL_DIR>/lib to WEB-INF/lib folder.

8. Make sure the web service class file copied to WEB-INF/classes as per it’s package structure.

9. Now build a war file and copy it to <TOMCAT_INSTALL_DIR>/webappas folder and start Tomcat.

That is it. Please check in the tomcat start-up logs to verify that the application is deployed properly with out any errors.

Once you make sure the server started with out errors, we can access the deployed web service at http://localhost:8080/SimpleWebService/SimpleWebService

The WSDL for the deployed web service is available at http://localhost:8080/SimpleWebService/SimpleWebService?wsdl

In the next article, we will learn what is WSDL and how is it used by web service consumers to create a web service client and access the web service.

An Eclipse Indigo source code project for this article can be downloaded from here.


From http://www.accordess.com/wpblog/2011/12/28/working-with-jax-ws-web-services-part-1/

APIs and microservices are maturing, quickly. Learn what it takes to manage modern APIs and microservices at enterprise scale.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}