Tuesday, September 02, 2008

Apache CXF: Open Source Service Framework

Web Service development has come a long way. I had some experience with XFire a few years ago and thought Web Service development could not get any easier. Then XFire became the Apache CXF project. I wanted to take a peak at Apache CXF so the other night I put together a simple echo Web Service. Alright, if you would like to follow along, you can create a Web Service in about 10 minutes:

First, we need our dependencies: (download them here)

...
commons-logging-1.1.1.jar
cxf-2.1.2.jar
FastInfoset-1.2.2.jar
geronimo-activation_1.1_spec-1.0.2.jar
geronimo-annotation_1.0_spec-1.1.1.jar
geronimo-javamail_1.4_spec-1.3.jar
geronimo-jaxws_2.1_spec-1.0.jar
geronimo-servlet_2.5_spec-1.2.jar
geronimo-stax-api_1.0_spec-1.0.1.jar
geronimo-ws-metadata_2.0_spec-1.1.2.jar
jaxb-api-2.1.jar
jaxb-impl-2.1.7.jar
jaxen-1.1.jar
jdom-1.0.jar
jetty-6.1.9.jar
jetty-util-6.1.9.jar
neethi-2.0.4.jar
saaj-api-1.3.jar
saaj-impl-1.3.jar
spring-beans-2.0.8.jar
spring-context-2.0.8.jar
spring-core-2.0.8.jar
spring-web-2.0.8.jar
stax-utils-20060502.jar
wsdl4j-1.6.2.jar
wstx-asl-3.2.4.jar
xml-resolver-1.2.jar
XmlSchema-1.4.2.jar
...

Second, we need our service interface: (note the @WebService annotation)

...
package com.thejavajar.service;

import javax.jws.WebService;

@WebService
public interface EchoServiceIfc {
    public String echo(String text);
}
...

Next, we need an implementation of our service interface: (note the @WebService annotation)

...
package com.thejavajar.service;

import javax.jws.WebService;

@WebService
public class EchoServiceImpl implements EchoServiceIfc {
    public String echo(String text) {
        return text;
    }
}
...

Finally, we can create a simple Java class that will start a server and expose our service:

...
package com.thejavajar.server;

import org.apache.cxf.frontend.ServerFactoryBean;
import com.thejavajar.service.EchoServiceIfc;
import com.thejavajar.service.EchoServiceImpl;

public class Server {
    public static void main(String[] args) {
        EchoServiceImpl echoServiceImpl = new EchoServiceImpl();

        // Create our Server
        ServerFactoryBean svrFactory = new ServerFactoryBean();
        svrFactory.setServiceClass(EchoServiceIfc.class);
        svrFactory.setAddress("http://localhost:9000/echo");
        svrFactory.setServiceBean(echoServiceImpl);
        svrFactory.create();
    }
}
...

Above, we set a few properties of the ServerFactoryBean that we have instantiated. We basically set up our service interface, our address (URL) to our service endpoint and the implementation class. We can run this as a normal Java application and browse over to http://localhost:9000/echo?wsdl and see our WSDL generated for our echo service.

There is nothing too complicated about the echo service. It is basically a Java interface, an implementation class and a few annotations. This is a great start for any developer who wants to start developing Web Services without getting overwhelmed with the full capabilities and features of Apache CXF or the spec. For more information, head on over to http://cxf.apache.org.

Eclipse Project: apache-cxf-echo-service.zip