Creating JAX-RS web service using Apache CXF Example

Ever since JAX-RS was introduced (JSR-311), it had a profound effect on the architecture and design of web services. It’s simplified scheme of creating an exposable service had really made an impact to how developers create web services as well as how it’s being used on the micro-service architecture. With this, a lot of service frameworks was introduced to create a more standardise and conventional way of following this scheme with more functional aspects to it. The list includes Jersey, RestEasy and for our topic of tutorial for this post: Apache CXF.

What is Apache CXF

Apache CXF is a service framework, that can be used by developers to create exposable and reusable web services. It supports almost every web service specification available and has a wide range of wrappers that can be used in binding, parsing and manipulating data. It’s extremely light weight and easy to plugin to any Java based applications.

Visit the project site here

For this scenario, we’re going to create a micro-service, deploy and consume it through the front-end app.

How do we build?

Let’s start with a scenario. Let’s try to expose a service (contract first) that will display an account detail given an account id. The service specs:

  • Service will accept account id as an input
  • Return the JSON format account id and name

Step by Step Guide

1. Create the Web App using Maven and download the Apache CXF dependencies

Download the Apache CXF distribution first, use the following Maven dependency on your project. Configure your project to enable all request to go through Apache CXF framework.

  1. Go to New > Maven Project
  2. Group Id: com.jcg.areyes.main
  3. Artifact Id: apache-cxf-sample
  4. Archetype Template: J2EE Simple WebApp





As it can be seen in the screenshot above, we imported the JAX-RS library from Apache CXF and http transport api that handles the transportation from process from start to end points.

2. Configure Apache CXF in the Web App.

The web app should be configured to use the Apache CXF Servlet for a specific URL path. This will allow the Apache CXF library to get the request and run all necessary process to create and call the service at runtime.


<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" xmlns=""

  <display-name>Archetype Created Web Application</display-name>

We configure our web.xml so that it will detect any URL to pass through the CXF service.


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

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


	<bean id="accountService" class="server.service.AccountService" init-method="init"></bean>
	<bean id="jsonProvider" 
  	<jaxrs:server id="accountrs" address="/rservice">
  			<ref bean="accountService"/>
			<ref bean='jsonProvider' />


This file is imported from web.xml. This holds the actual services and the jaxrs service declaration.

3. Create the Service Implementation on the App

Create the Account Object first. This is the object that we will return from our service.

package server.obj;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "Account")
public class Account {
    private long id;
    private String name;

    public long getId() {
        return id;

    public void setId(long id) { = id;

    public String getName() {
        return name;

    public void setName(String name) { = name;

Create the Service Implementation class. This will house our actual implementation.

package server.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import server.obj.Account;

public class AccountService {

	Map<String, Account> accounts = new HashMap<String, Account>();

	public void init() {

		Account newAccount1 = new Account();
		newAccount1.setName("Alvin Reyes");

		Account newAccount2 = new Account();
		newAccount2.setName("Rachelle Ann de Guzman Reyes");

		accounts.put("1", newAccount1);
		accounts.put("2", newAccount2);


	public AccountService() {

	public Account getAccount(@PathParam("id") String id) {
		Account c = accounts.get(id);
		return c;

	public List getAllAccounts(Account account) {
		List accountList = new ArrayList();
		for (int i = 0; i <= accounts.size(); i++) {
			accountList.add((Account) accounts.get(i));
		return accountList;


As it can be seen above, there’s a bunch of Annotations that we used. These annotations are crucial as it allows the JVM to categorize this source code to have a configuration injected to it and Apache CXF to notice that we are using its libraries. The following annotations are described below:

  • @Path – The endpoint url path.
  • @Get – This means that the method is called using the GET http method.
  • @Produce – the response output format.
  • @QueryParam – Query parameters passed via the URL.

4. Test out the service

Once everything is in place, we can now test the service. Test it via your browser or a chrome extension (Poser).

Figure 4.0 Json Output from the REST service call


Download the Eclipse project of this tutorial:

This was an example of creating a JAX-RS compliant web service using Apache CXF

You can download the full source code of this example here:

Alvin Reyes

Alvin has an Information Technology Degree from Mapua Institute of Technology. During his studies, he was already heavily involved in a number of small to large projects where he primarily contributes by doing programming, analysis design. After graduating, he continued to do side projects on Mobile, Desktop and Web Applications.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Newest Most Voted
Inline Feedbacks
View all comments
5 years ago

On which Web server we need to run this application??

5 years ago
Reply to  vijay

any webserver like tomcat, weblogic, websphere

4 years ago

why do you use: ?

Back to top button