RESTful Web Service with Apache CXF for a Java 5 Web Site

Dealing with legacy code is painful, but someone has to do it. I had to make a Restful WS (JSON objects passed everywhere) within a webapp supporting Java 5 and Spring 2.0.3 (a very old version!).

Recently I’ve been investigating about making WS with a framework called AppFuse, which implements the Apache CXF library to make SOAP and REST WS. AppFuse has an interesting documentation and it’s easy to read. But Apache’s library stopped supporting Java 5 since the version 2.7, so I had to configure the Servlet (web.xml file) circumventing the Spring Containter and declare all my JAX-RS Services there.

Here is an example of the implementation:

Requirements for the WebApp:

  • Java 5 (you can target Java 6 to compile to 1.5)
  • Maven 2.2.1 (for dependency management)
  • Apache CXF 2.6.8
  • Spring 2.0.3
  • Jackson-RS (which is tightly integrated to Apache CXF and provides an easy implementation of JSON Reading/Writing)

First configure pom.xml file to download the dependencies:


After you updated the pom.xml, you’ll need to declare and define a CXFNonSpringJaxrsServlet in your web.xml file. Inside this servlet, we will declare all our Service implementations (comma separated) and finally, specify our jsonProvider implementation :



After this all you need is to create the Service class with annotations (yeah, Java 5 supports annotations, yay!):

package com.wordpress.controlpblog.service.json;


public class HelloService {

	public String sayHello(){
//this will say Hello also in the Console
		System.out.println("Hello World");
		return "Hello World";

Finally, make the war file and deploy the application in your server of choice (I have an old JBOSS 4):

 mvn clean package

and test the response doing this command for the Web Service final URL:

 curl localhost:8080/myApp/rest/sample/hello


Hello World

Boom, roasted! Now you can implement all your JSON Services specifying them with @Path annotation and in an easy way to support as legacy.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s