Posted by: gorik | 15 March, 2008

JAX-WS and Jetty

JAX-WS and Jetty

Here is an example on how I used Jetty with JAX-WS.
I’m using Java 6, Jetty 6.1.3 and JAX-WS 2.1.

Setup Jetty

First we need to setup Jetty. You can find Jetty at http://www.mortbay.org/.

I created a structure like this:

jetty/
etc/
logs/
webapps/
lib/
jetty.xml

From the Jetty libs, I only used jetty-6.1.x.jar, jetty-util-6.1.x.jar and the jars in the jsp-2.1 subfolder.
In etc I only kept webdefault.xml.
logs and webapps are empty for now (but you need to create the folders).

Here is the code you need to run this server (if you are running jetty from source).

Server server = new Server();
XmlConfiguration configuration = new XmlConfiguration(

new FileInputStream(”jetty/jetty.xml”));
configuration.configure(server);
server.start();

That’s it.

Make sure you configure jetty.xml to point to the correct locations.

This are some of my settings:


<set name=”configurationDir”><systemproperty name=”jetty.home” default=”./jetty”>/contexts</systemproperty>

<set name=”webAppDir”><systemproperty name=”jetty.home” default=”./jetty”>/webapps</systemproperty>

<set name=”defaultsDescriptor”><systemproperty name=”jetty.home” default=”./jetty”>/etc/webdefault.xml</systemproperty></set></set></set>

Now we have to create JAX-WS services and make them work inside Jetty.

Creating a Web service using JAX-WS

As for JAX-WS, I generated java from a wsdl file using wsimport. Once you done this you only need to implement the service endpoint.
I started from the addNumbers sample that is provided with the jax-ws package.
If you decide to move the generated code to another package, make sure you also change all the annotations in the code.

Now I made a folder for my webservice, f.i. test, that looks like this:

test/
WEB-INF/
wsdl/
web.xml

I put the AddNumbers.wsdl in the wsdl folder and I made a web.xml like this:

<web-app version=”2.4″ mlns=”http://java.sun.com/xml/ns/j2ee” xsi:schemalocation=”http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd”>
<description>fromwsdl</description>
<display-name>fromwsdl</display-name></web-app>
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class></listener> <servlet>
<description>JAX-WS endpoint – fromwsdl</description>
<display-name>fromwsdl</display-name>
<servlet-name>AddNumbersPort</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>AddNumbersPort</servlet-name>
<url-pattern>/addnumbers</url-pattern>
</servlet-mapping>

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

Make sure the servlet-name and matches what’s in your source code.

If you put the test folder in the Jetty webapps folder and start Jetty you should see something when you browse to http://localhost:8080.
A good tool to test your webservice is SoapUI. There is a free and a commercial version of this tool, the free version did everything I needed.


Leave a response

Your response:

Categories