<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>By Programmer For Programmer &#187; annotations</title>
	<atom:link href="http://byprogrammerforprogrammer.com/tag/annotations/feed/" rel="self" type="application/rss+xml" />
	<link>http://byprogrammerforprogrammer.com</link>
	<description>Here I lay down the useful tips, tricks and utilities for programmers like myself.</description>
	<lastBuildDate>Thu, 15 Sep 2011 19:59:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Java Web Service Annotations in Tomcat</title>
		<link>http://byprogrammerforprogrammer.com/2009/11/java-web-service-annotations-in-tomcat/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=java-web-service-annotations-in-tomcat</link>
		<comments>http://byprogrammerforprogrammer.com/2009/11/java-web-service-annotations-in-tomcat/#comments</comments>
		<pubDate>Wed, 04 Nov 2009 19:56:06 +0000</pubDate>
		<dc:creator>Sean Adkinson</dc:creator>
				<category><![CDATA[Example Code]]></category>
		<category><![CDATA[annotations]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[tomcat]]></category>
		<category><![CDATA[web services]]></category>

		<guid isPermaLink="false">http://byprogrammerforprogrammer.com/?p=43</guid>
		<description><![CDATA[Easy-to-follow instructions on how to go from java files with web service annotations (@WebService, @WebMethod, @WebParam, etc) to automatic generation of a WSDL in Tomcat.]]></description>
			<content:encoded><![CDATA[<p>So recently I had to build a web service, and had a really hard time finding documentation or instructions on how to make web services work in Tomcat using the web service annotations from <a href="http://jcp.org/en/jsr/summary?id=181" target="_blank">JSR 181</a> (@WebService, @WebMethod, @WebParam, etc).  So I created this post as a way to help someone else in the same situation.  I&#8217;m sure there are many ways to do this, but this is one way.</p>
<p>My goal was to create java files annotated with web service annotations and deploy the application to Tomcat, and just have everything magically work.  I didn&#8217;t want to use the wsgen tool.  I wanted it to work like it does in JBoss, Weblogic, and Glassfish, where you just deploy the application and the web service endpoints are created automatically.</p>
<h2>Environment</h2>
<p>Here is the environment that I was working with:</p>
<ul>
<li>Java 1.6.0_16</li>
<li>Tomcat 6.0.18</li>
<li>Eclipse 3.4</li>
<li>Building a WAR and dropping into webapps folder in Tomcat</li>
</ul>
<h2>Example Class</h2>
<p>MyWebService.java:</p>
<pre>
package com.example.webservice;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

/**
 * An example web service implementation
 * @author Sean Adkinson
 */
@WebService(targetNamespace = "http://com.example.webservice", name = "mywebservice")
public class MyWebService
{

    /**
     * Says hello to the given name
     * @param name A name
     * @return Hello, name
     */
    @WebMethod(operationName = "hello")
    @WebResult(name = "return")
    public String hello(@WebParam(name = "name") String name)
    {
        return "Hello, " + name;
    }

}
</pre>
<h2>Instructions</h2>
<p>Follow these instructions to create a web service from our example class above:</p>
<ol>
<li>Download Metro 1.5 binary (or latest point release) at https://metro.dev.java.net/1.5/</li>
<li>Run <code>java -jar metro-1_5.jar</code> to unpack the contents of the jar</li>
<li>Take all the lib/webservices-*.jar files from the expanded Metro installation, and copy them into your web application&#8217;s WEB-INF/lib folder.  Note that I chose to put these in my application&#8217;s library, but you can also put them in the server&#8217;s library at tomcat.home/lib.</li>
<li>Create a <strong>sun-jaxws.xml</strong> file right next to web.xml under WEB-INF.  This is where you specify where to find the classes that have your web service annotations.  Here is a sample file:<br />
<br />
sun-jaxws.xml:</p>
<pre>
&lt;endpoints xmlns='http://java.sun.com/xml/ns/jax-ws/ri/runtime' version='2.0'&gt;
    &lt;endpoint
        name='mywebservice'
        implementation='com.example.webservice.MyWebService'
        url-pattern='/mywebservice' /&gt;
&lt;/endpoints&gt;
</pre>
<p>Here, <strong>name</strong> can be anything, <strong>implementation</strong> is the classpath reference to the class with the web service annotations that should have a web service created from it, and <strong>url-pattern</strong> is the path after your web application&#8217;s URL that will hit this web service (so if your application is at http://localhost:8080/MyApplication, this web service will be available at http://localhost:8080/MyApplication/mywebservice).
</li>
<li>Edit your <strong>web.xml</strong> to map the URL pattern above to class <strong>com.sun.xml.ws.transport.http.servlet.WSServlet</strong>, and add a listener for class <strong>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</strong>.  Here is an example:
<pre>
&lt;listener&gt;
    &lt;listener-class&gt;com.sun.xml.ws.transport.http.servlet.WSServletContextListener&lt;/listener-class&gt;
&lt;/listener&gt;
&lt;servlet&gt;
    &lt;servlet-name&gt;myws&lt;/servlet-name&gt;
    &lt;servlet-class&gt;com.sun.xml.ws.transport.http.servlet.WSServlet&lt;/servlet-class&gt;
    &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
&lt;/servlet&gt;
&lt;servlet-mapping&gt;
    &lt;servlet-name&gt;myws&lt;/servlet-name&gt;
    &lt;url-pattern&gt;/mywebservice&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;
</pre>
</li>
<li>Now deploy your webapp, start Tomcat, and go to http://localhost:8080/MyApplication/mywebservice, and you should see details about the endpoint that was created, with a link to the generated WSDL file.</li>
<li>From here you should be able point whatever client code generating utilities you have at the WSDL and talk with the web service from a client.  I use built-in Eclipse Axis2 code generation for this, but check out wsimport in the Metro /bin folder for another option.</li>
</ol>
<p>That&#8217;s it!  Hope this helps someone who had as hard a time as I did with getting this working in Tomcat.</p>
]]></content:encoded>
			<wfw:commentRss>http://byprogrammerforprogrammer.com/2009/11/java-web-service-annotations-in-tomcat/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

