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:

<dependency>
	<groupId>org.apache.cxf</groupId>
	<artifactId>cxf-rt-frontend-jaxrs</artifactId>
	<version>2.6.8</version>
</dependency>
<dependency>
	<groupId>org.apache.cxf</groupId>
	<artifactId>cxf-rt-transports-http</artifactId>
	<version>2.6.8</version>
</dependency>
<dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-rs-extension-providers</artifactId>
      <version>2.6.8</version>
</dependency>
<dependency>
	<groupId>org.codehaus.jackson</groupId>
	<artifactId>jackson-jaxrs</artifactId>
	<version>1.1.1</version>
</dependency>

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 :

<servlet>
 <servlet-name>CXFServlet</servlet-name>
 <servlet-class>
  org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet
 </servlet-class>
 <init-param>
 <param-name>jaxrs.serviceClasses</param-name>
 <param-value>
  com.wordpress.controlpblog.service.json.HelloService
 </param-value>
 </init-param>
 <init-param>
  <param-name>jaxrs.providers</param-name>
  <param-value>
   org.codehaus.jackson.jaxrs.JacksonJsonProvider   
  </param-value>
 </init-param>
</servlet>

<servlet-mapping>
 <servlet-name>CXFServlet</servlet-name>
 <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

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;

import javax.ws.rs.*;

@Path("/sample")
public class HelloService {

	@GET
	@Path("hello")
	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

Response:

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.

Regards

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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