Archive for January, 2008

POX (Plain Old XML) JAX-WS service using CXF and Spring Configuration

The new and upcoming Apache CXF framework is quickly gaining steam and rightfully so. It’s very intuitive, simple to use and functional. Some of it comes from the fact the its roots are in popular XFire and Celtix frameworks. It’s still in the incubation stage at Apache but the current releases are pretty stable. The documentation is not complete right now, but it’s improving as well.


One of the cases where I found the document and example lacking is the POX (Plain Old XML) service using JAX-WS Provider model and configuring it using Spring. The examples are mainly focussed towards configuring and starting the service from Java code. So, I created this simple EchoService example that shows step by step how to do it.

First step is to create the service Java class that will echo the incoming XML message back to the requester. We will also annotate this class using the JAX-WS annotation to mark it as a web service provider and the message type to be the payload. So, the CXF implementation knows that it should only deliver the message payload to the service class. The CXF binding layer processes any binding level wrappers and headers.

In the above class we specified that we want to receive the incoming XML message as DOM. You can change it to use any concrete subclass of the Source interface (e.g. SAXSource).


Next step is to create the Spring configuration. For simplicity sake, let’s keep the name of this file to applicationContext.xml. Here is an example:

The key things to notice in the above XML are:

  • Address of the service (/echo).
  • Binding URI (http://www.w3.org/2004/08/wsdl/http). This is really important. This is what tells CXF that this service is going to be simple XML over HTTP.
  • Declaration of service factor where we indicate that the messages are going to be of wrapped nature

All that’s remaining is to declare the Spring context listener in your web.xml for the web application that this service is going to be a part of. Here is an example of the web.xml:

As you can see that there is nothing special here at all. It’s all standard Spring configuration using the context loader listener. Also, we are declaring the CXF servlet here and mapping it to /* to make all the requests to this web application go through the CXF servlet.

Now all you have to do is to package this web application and deploy it in your favorite application server. You should be able to access the Echo Service we wrote at http://localhost:8080/AppName/echo (you will have to adjust the port and application name in the URL). Now HTTP post a XML to this URL and you should get the request echoed back to you.

Maven POM
I think it’s more useful to also specify the Maven POM that contains all the dependencies as well to build the run this example. Please keep in mind that some of the dependencies here may not be required anymore as I was playing around with a lot of CXF functionalities.

Here is a list of all the jar files that ended up being in the war file from the build using above Maven POM. Again, some of the jars here may not be needed.

Share and Enjoy:
  • Digg
  • del.icio.us
  • description
  • Technorati
  • Reddit
  • Facebook
  • blogmarks
  • YahooMyWeb
  • Ma.gnolia

Comments (2)