Produce Json From Restful CXF Web Service

In this post, we are going to create a simple Restful web service that returns json.
This post follows on from Create Restful Web Services with JBoss, Maven, Apache CXF and Spring, but is also self-contained.

Create the Project

Using Eclipse / JBoss Developer Studio

  • New -> Maven Project
  • maven-archetype-webapp
  • Group Id: com.skills421.webservices
  • Artifact Id: SimpleCXFRest

The default project structure will be created as follows:


Add Missing Folders and Delete index.jsp

  • Right-click src/main
  • New -> Folder -> java
  • Delete src/main/webapp/index.jsp

Add Maven Dependencies

In order to return json from our web service, we are going to use Jackson for our json mapping. We need to edit the pom.xml to add:

<!-- Jackson -->

This gives a full the pom.xml as follows:

<project xmlns="" xmlns:xsi=""
<name>helloweb Maven Webapp</name>


<!-- Spring -->


<!-- CXF -->

<!-- Jackson -->

<!-- JUnit -->


Update the Project

Right-click on the project and select Maven -> Update Project.

Modify web.xml

Modify web.xml to include the servlets as follows:

"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"" >

<display-name>Simple CXF Rest</display-name>

<!-- Spring -->


<!-- CXF -->
<display-name>CXF Servlet</display-name>


Create beans.xml

In the WEB-INF folder, create the file beans.xml.
In order to return Json we need to add our JacksonJsonProvider bean, giving us a beans.xml as follows:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
xmlns:xsi="" xmlns:jaxrs=""
xmlns:jaxws="" xmlns:context=""

<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

<context:component-scan base-package="" />

<!-- JAX-RS -->
<jaxrs:server id="restContainer" address="/">
<ref bean="simpleRestService" />
<ref bean="customerService" />
<bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"/>


<!-- -->

Create the Restful Service

In src/main/java create the following class in the correct package



import org.springframework.stereotype.Service;

import com.skills421.model.Customer;

public class CustomerService
public Response find(@PathParam("id") int id)
Customer customer = new Customer(1,"Jon Doe",21);

return Response.status(Status.OK).entity(customer).type(MediaType.APPLICATION_JSON).build();

Run the Project

Right-click on the project -> Run As -> Run on Server
select a server to run on, (Tomcat 7 will run without errors).

Test the Service

Open the web browser and enter the following url:

Your web browser should display the following:

{“id”:1,”name”:”Jon Doe”,”age”:21}

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 )

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

Up ↑

%d bloggers like this: