This is the first of a two part series showing how easy it is to expose and consume web services using the Spring Framework and XFire. In this part, we’ll be going through exposing the web services. This assumes you are using the following:

- Maven 2 (for dependencies)
- Spring 2.0 (although, it should work fine in 1.2 as well)
- Java 5 (for the JSR 181 annotations)

First, we create several projects:

- Domain model
- Service Interfaces
- Service Implementation
- Reference Implementation

The purpose of separating out the domain model and service interfaces is that it gives us flexibility later on. For example, we’ll include them both in the reference implementation (part II) in order to consume the web services more easily. You could combine the service and domain projects, but for most cases, I’ve found separating them makes things easier down the road. Creating the domain model is simple and not really related to web services, so we’ll mostly ignore it in this tutorial. We’ll just assume it has a simple ‘Hello’ object in it that looks like this:

package net.uresk.ws

class Hello {

private String hello = “hello”;

public String getHi(){
return hello;
}

}

Now, let’s create our service interface. Also very simple - one class.

package net.uresk.ws;

import javax.jws.WebService;

@WebService
public Interface HelloService {

public Hello getHello();
}

Notice how simple it is? Of course, there are many other options you can configure, but one annotation is all that is required. We’ll need to implement this interface and expose it in our webservice project:

package net.uresk.ws;

import javax.jws.WebService;

@WebService
public HelloServiceImpl implements HelloService {

public Hello getHello(){

return new Hello();
}

}

Now that we’ve provided an implementation, let’s expose it in our spring configuration file:

<import resource=”classpath:org/codehaus/xfire/spring/xfire.xml”/>

<bean id=”xfire.annotationServiceFactory” class=”org.codehaus.xfire.annotations.AnnotationServiceFactory” />

<bean id=”genericServiceBean” class=”org.codehaus.xfire.spring.ServiceBean” abstract=”true”>
<property name=”serviceFactory” ref=”xfire.annotationServiceFactory” />
</bean>

<bean id=”HelloServiceImpl” class=”net.uresk.ws.HelloServiceImpl” />

<bean id=”HelloService” parent=”genericServiceBean”>
<property name=”serviceBean” ref=”HelloServiceImpl” />
</bean>

In the first line, we are importing some necessary xfire configuration.

Next, we specify the annotationServiceFactory to use the JSR-181 annotations.

The next definition is a generic service bean. This is an abstract spring bean we’ll base all of our services off. This makes it easy to add in things such as security later on.

Next, we setup the HelloService implementation.

Last, we inject the HelloServiceImpl into our generic service bean.

That’s it! Now, we need to setup our XFire servlet in our web.xml:

<servlet>
<servlet-name>XFireServlet</servlet-name>
<display-name>XFire Servlet</display-name>
<servlet-class>
org.codehaus.xfire.transport.http.XFireConfigurableServlet
</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>XFireServlet</servlet-name>
<url-pattern>/servlet/XFireServlet/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>XFireServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>

You’re done. Your wsdl should be located at

http://localhost:8080/<project>/services/HelloServiceImpl?wsdl.

The service url is - http://localhost:8080/<project>/services/HelloServiceImpl.

In part 2, we’ll look at how easy it is to consume this service. If you are using maven and are keeping the projects separate, doing so will be easy.