Deploy JAX-WS web services on Tomcat

Here’s a guide to show you how to deploy JAX-WS web services on Tomcat servlet container. See following summary steps of a web service deployment.

  1. Create a web service (of course).
  2. Create a sun-jaxws.xml, defines web service implementation class.
  3. Create a standard web.xml, defines WSServletContextListener, WSServlet and structure of a web project.
  4. Build tool to generate WAR file.
  5. Copy JAX-WS dependencies to “${Tomcat}/lib” folder.
  6. Copy WAR to “${Tomcat}/webapp” folder.
  7. Start It.

Directory structure of this example, so that you know where to put your files.

jaxws-deploy-tomcat--folder

1. WebServices

A simple JAX-WS hello world example.

File : HelloWorld.java


package com.mkyong.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

//Service Endpoint Interface
@WebService
@SOAPBinding(style = Style.RPC)
public interface HelloWorld{
	
	@WebMethod String getHelloWorldAsString();
	
}

File : HelloWorldImpl.java


package com.mkyong.ws;

import javax.jws.WebService;

//Service Implementation Bean

@WebService(endpointInterface = "com.mkyong.ws.HelloWorld")
public class HelloWorldImpl implements HelloWorld{

	@Override
	public String getHelloWorldAsString() {
		return "Hello World JAX-WS";
	}
}

Later, you will deploy this hello world web service on Tomcat.

2. sun-jaxws.xml

Create a web service deployment descriptor, which is also known as JAX-WS RI deployment descriptor – sun-jaxws.xml.

File : 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="com.mkyong.ws.HelloWorldImpl"
      url-pattern="/hello"/>
</endpoints>

When user access /hello/ URL path, it will fire the declared web service, which is HelloWorldImpl.java.

Note
For detail endpoint attributes , see this article.

3. web.xml

Create a standard web.xml deployment descriptor for the deployment. Defines WSServletContextListener as listener class, WSServlet as your hello servlet.

File : web.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, 
Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">

<web-app>
    <listener>
        <listener-class>
                com.sun.xml.ws.transport.http.servlet.WSServletContextListener
        </listener-class>
    </listener>
    <servlet>
        <servlet-name>hello</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>hello</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>120</session-timeout>
    </session-config>
</web-app>

4. WAR Content

Use Ant, Maven or JAR command to build a WAR file to include everything inside. The WAR content should look like this :


WEB-INF/classes/com/mkyong/ws/HelloWorld.class
WEB-INF/classes/com/mkyong/ws/HelloWorldImpl.class
WEB-INF/web.xml
WEB-INF/sun-jaxws.xml
Note
For those who are interested, here’s the Ant file to build this project and generate the WAR file.

File : build.xml


<project name="HelloWorldWS" default="dist" basedir=".">
    <description>
        Web Services build file
    </description>
  <!-- set global properties for this build -->
  <property name="src" location="src"/>
  <property name="build" location="build"/>
  <property name="dist"  location="dist"/>
  <property name="webcontent"  location="WebContent"/>

  <target name="init">
        <!-- Create the time stamp -->
        <tstamp/>
        <!-- Create the build directory structure used by compile -->
        <mkdir dir="${build}"/>
  </target>

  <target name="compile" depends="init"
  	description="compile the source " >
        <!-- Compile the java code from ${src} into ${build} -->
        <javac srcdir="${src}" destdir="${build}"/>
  </target>

  <target name="war" depends="compile"
  	description="generate the distribution war" >
	    
	<!-- Create the war distribution directory -->
  	<mkdir dir="${dist}/war"/>
    
  	<!-- Follow standard WAR structure -->
  	<copydir dest="${dist}/war/build/WEB-INF/" src="${webcontent}/WEB-INF/" />
  	<copydir dest="${dist}/war/build/WEB-INF/classes/" src="${build}" />
  		
	<jar jarfile="${dist}/war/HelloWorld-${DSTAMP}.war" basedir="${dist}/war/build/"/>
  </target>
  
</project>

5. JAX-WS Dependencies

By default, Tomcat does not comes with any JAX-WS dependencies, So, you have to include it manually.

1. Go here http://jax-ws.java.net/.
2. Download JAX-WS RI distribution.
3. Unzip it and copy following JAX-WS dependencies to Tomcat library folder “{$TOMCAT}/lib“.

  • jaxb-impl.jar
  • jaxws-api.jar
  • jaxws-rt.jar
  • gmbal-api-only.jar
  • management-api.jar
  • stax-ex.jar
  • streambuffer.jar
  • policy.jar

6. Deployment

Copy the generated WAR file to {$TOMCAT}/webapps/ folder and start the Tomcat server.

For testing, you can access this URL : http://localhost:8080/HelloWorld/hello, if you see following page, it means web services are deploy successfully.

jaxws-deploy-tomcat--example

Download Source Code

Download It – JAX-WS-Deploy-To-Tomcat-Example.zip (13KB)

Reference

  1. JAX-WS WAR File Packaging
  2. Deploying Metro endpoint
  3. Publishing a RESTful Web Service with JAX-WS

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
92 Comment threads
43 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
102 Comment authors
CristianpsnMikesMichael SheligaCarlos Recent comment authors
newest oldest most voted
Fremont
Guest
Fremont

i use latest jaxws-ri-2.x.jar. I found ha-api.jar and jaxb-core are also required.

Yogesh Malhotra
Guest
Yogesh Malhotra

If I understood it correctly, sun-jaxws.xml and servlet config is required in case of deploying web service to tomcat server only.

Kuba
Guest
Kuba

Hi,
did you try to use JAX-WS with digital sinature? I mean solution described in Metro guide: http://jax-ws.java.net/guide/Configuring_Keystores_and_Truststores.html and it doesn’t work for me.

sijodoh
Guest
sijodoh

thanks, I’ll try if there is trouble I will be asking his opinion.

sampath kumar
Guest
sampath kumar

you specifies @SOAPBinding(style = Style.RPC)

i need to know what are styles are applicable and i need an example for Rest style webservice example , and what are the main difference between soap based webservice and REST based web service and how to perform authentication in soap style and rest style

Manish
Guest
Manish

Hi,

I like your site …
Nice and clear explanation…..

Keep it up.

trackback
Deploy JAX-WS web services on Tomcat + SSL connection

[…] Deploy it like a normal web service, see this guide – Deploy JAX-WS web services on Tomcat servlet container. […]

trackback
Container Authentication with JAX-WS – (Tomcat version)

[…] See this detail guide on how to deploy JAX-WS web services on Tomcat. […]

Abdul Mannan
Guest
Abdul Mannan

Short but everything. Excelent!!

Melinda
Guest
Melinda

I was able to complete this tutorial, Deploy JAX-WS web services on Tomcat, successfully and I wanted to say thank you. But I had one question. When I clicked on the WSDL link I do NOT see the names of the params within the but only the following and I needed to know how to get the names of each param instead of arg1 – arg14 that was depicted in the WSDL? Here is what came out in the WSDL:

And within the :

Any help/direction would be greatly appreciated. Thank you.

Mahendra
Guest
Mahendra

Short and sweet explanation….
My problem is I am not able to find the above given dependency jars, pls can you help me.

-Mahendra

Mahendra
Guest
Mahendra

Thanks for reply..
One more problem I am facing while creating war..
It is not able find the JAR file refference when i run ‘ant war’ command.

Getting Error:
package javax.jws does not exist
import javax.jws.WebMethod;

How can I give refference while creating war?

Also I have stored all jars in folder called ‘lib’ and I have adde following comd in build file

But still it is not able to get that jars..

Please help me on the same…
-Mahendra

trackback
JAX-WS + Java Web Application Integration Example

[…] How to deploy JAX-WS on Tomcat […]

john
Guest
john

Hi your blog is very useful,but when i place war file in webapps,it is not automaically deployed.when i press start it shows the error “FAIL – Application at context path /HelloWorld could not be started”.what is the issue

djitz
Guest
djitz

Thanks a lot for the tutorial!

However when I tried this on Eclipse I need to add more one jar file from JAX-WS RI distribution, that is ha-api.jar. 😀

Lal ajith kumara
Guest
Lal ajith kumara

Hi Team,

I’m getting following error when i tried to run the client , could you help me ?

Exception in thread “main” javax.xml.ws.WebServiceException: Failed to access the WSDL at: http://localhost:8666/HelloWorld/hello?wsdl. It failed with:
Connection refused: connect.
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.tryWithMex(RuntimeWSDLParser.java:151)
at com.sun.xml.internal.ws.wsdl.parser.RuntimeWSDLParser.parse(RuntimeWSDLParser.java:133)
at com.sun.xml.internal.ws.client.WSServiceDelegate.parseWSDL(WSServiceDelegate.java:254)
at com.sun.xml.internal.ws.client.WSServiceDelegate.(WSServiceDelegate.java:217)
at com.sun.xml.internal.ws.client.WSServiceDelegate.(WSServiceDelegate.java:165)
at com.sun.xml.internal.ws.spi.ProviderImpl.createServiceDelegate(ProviderImpl.java:93)
at javax.xml.ws.Service.(Service.java:92)
at javax.xml.ws.Service.create(Service.java:722)
at com.mkyong.client.HelloWorldClient.main(HelloWorldClient.java:23)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)

Edwin
Guest
Edwin

Hi! I am trying to run this example uisng java6 and Tomcat6 but when I compile in linux I get this error: root@squezze:~/wshex# javac src/com/mkyong/ws/HelloWorldImpl.java src/com/mkyong/ws/HelloWorldImpl.java:8: cannot find symbol symbol: class HelloWorld public class HelloWorldImpl implements HelloWorld{ ^ src/com/mkyong/ws/HelloWorldImpl.java:10: method does not override or implement a method from a supertype @Override ^ 2 errors Anybody has a cluee ?

geo
Guest
geo

Hi,

thank you for the excellent tutorial!

Perhaps only a supplement, I had to copy the ha-api.jar to Tomcat library folder.

Geo

sara a
Guest
sara a

hello
plaese help me
i want implement ecommerce with metro web services
I dont know what i do?

andist
Guest
andist

Hi,
thank you for this really good tutorial.
I have a problem in the final step. I putted the war-File in the webapps-folder and started tomcat. The war-File is now unpacked in the webapps-folder, but I can’t open the URL in browser (HTTP Status 404). There were no errors during the start of tomcat.
Hope someone can help me. Thanks!

mstamim
Guest
mstamim

Really thanks for this clear, simple, and useful tutorial and example, it saved me a lot of time and took me to the point

turksson
Guest
turksson

Thank you for this great tutorial!
If someone tries to start the tomcat and gets an error like “BackingStoreException… class not found…”, download the ha-api.jar and copy it in the {$tomcat}/lib order and try it again.

FallenAngel
Guest
FallenAngel

Thank you for this clear and well laid out tutorial. It took me days to get a webservice up a running and all that was missing was the jar files at the end of the tutorial in tomcat/lib. Didn’t see that explain on any other site.

Thanks for your help, keep up the good work!

Iain
Guest
Iain

Great tutorial, this was extremely helpful!

Rajaneesh
Guest
Rajaneesh

I have a question:
Intially, I have developed a web service using JDK1.5.
Now, I would like to upgrade it to jdk1.6. Will there be any change the wsdl generated?

Please let me know as soon as possible.

vikas
Guest
vikas

Thanks for nice tutorial with perfect example !!!

vinit
Guest
vinit
Hi,
   Using JAX-WS api i have created a ejb webservice.
 When i deploy it on jboss5.0, 
Jboss generated url : http://localhost:8080/demoContext/DemoBean?wsdl

when i deploy it on Tomcat(Integrated with OpenEjb)

OpenEjb generated url : http://localhost:8080/DemoBean?wsdl

so can you help me to make the tomcat generated url as same as Jboss generated url.

Pankaj Soni
Guest
Pankaj Soni

I appreciate your tutorial.
It will be more useful if you show this in a maven project as your most of the tutorials. So, that we will not face the dependency related issues.

Vishwanath
Guest
Vishwanath

Hi,

Thanks very much for this tutorial this was very helpful in implementing the webservice.

However when we build a war file it creates in the following format.
HelloWorld-${DSTAMP}.war

so in that case in order to access the webpage this would be the url

http://localhost:8080/${warFileName}/hello
eg:
http://localhost:8080/HelloWorld-20120510/hello

Here the war file that is created after running the build is having its name as
HelloWorld-20120510.
This is how it extracts the war file in the webapps folder.

please correct me if any thing is wrong .

Thanks
Vishwanath

Nikhil
Guest
Nikhil

Hi

I am confused as to how you began this project in Eclipse. Is this project meant to be a “Dynamic Web Project” ? I have tried many different wizards i.e. Dynamic Web Project, Web Service, Java Project but yet I am not able to get the same directory structure as your example. Any help will be appreciated here.

Thanks