Saturday, 15 February 2014

REST with Apache CXF


Apache CXF is a front-end framework for JAX-WS and JAX-RS services. 
Here in tutorial I am going to show how to develop restful service with Apache CXF.

Building your first REST Application

Though I have used IntelliJ IDEA to write this application, but you can use any other IDE like Eclipse or Netbeans to write it. This application will use Apache Maven for its build and dependency management.

1. Create your project

We will create a project named TodoApplication. Since this will be a maven project, so the project structure is going to be pretty standard as shown below:

 Don't mind the .idea folder since it is IDE specific. Rest should look as is.

2. Defining dependencies in pom.xml

For Apache CXF, there is a bundle jar which contains everything for JAX-RS implementation. Add the following dependency in pom.xml


For logging we will use slf4j over log4j.


And lastly dependencies for Spring


The versions for slf4j, log4j and spring are : 


3. Defining web.xml

In folder src/main/webapp create a new folder with name WEB-INF. Under this folder create a file web.xml which defines your servlets and their mappings. CXF under the hood uses servlet only to map the urls to restful resources. Following is how our web.xml will look

 <?xml version="1.0" encoding="UTF-8"?>  
 <web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="2.5">  

CXFServlet is the servlet that dispatches the http call to restful resources. cxf-context.xml has the rest endpoints defined in it. We will get there in a while.

4. Writing a Rest Service

A Rest Service is basically the service class that will be exposed as endpoints. These have JAX-RS annotations that define the service. For starters let's write a Ping service which will simply tell that whether application is running fine or not.

 import org.slf4j.Logger;  
 import org.slf4j.LoggerFactory;  
 public class PingService {  
   Logger logger = LoggerFactory.getLogger(PingService.class);  
   public Response ping(){"Received request for ping service");  
     return Response.ok("Application is running fine").build();  

@Path annotation defines the path at which this service will be exposed.
@GET bind ping method to HTTP GET call. There are similar annotation for PUT, POST, DELETE and OPTIONS.
@Produces defines the Content-Type of the response.

5. Defining cxf-context.xml

This context file contains the configuration for cxf bus i.e. the transport and the endpoints.

 <beans xmlns=""  
   <bean id="pingService" class=""/>  
   <jaxrs:server id="restContainer" address="/">  
       <ref bean="pingService"/>  

<jaxrs:server> contains the service endpoints and the provider entries. Service endpoints goes in the <jaxrs:serviceBeans>

<cxf:bus> contains the configuration for cxf bus. Like logging is one of the features which will log every incoming request and outgoing response.

6. Deploying the application

This application can be deployed on one JEE container like Tomcat, Jetty.

After deployment hit the following url in your browser :


You will see "Application is running fine" message in the browser.