Exposing CXF Web services with Camel

In this tutorial we will learn how to create a simple Camel CXF Consumer from a Java interface (code first approach)

 So your starting point is the following interface that defines the Contract which will be exposed through the CXF Component:

package com.mycompany.service;

public interface CustomerService {
	public Customer findCustomer(String cId);
	public String deleteCustomer(String cId);

Exposing this interface through Camel is trivial, all you have to do is including the CXF Component in your Camel XML file (or your Java DSL component):

 <cxf:cxfEndpoint id="customerEndpoint" address="http://localhost:9001/customerService" serviceClass="com.mycompany.service.CustomerService" />

This will create a WSDL contract at the following address: http://localhost:9001/customerService?wsdl

Next step will be routing the request to an implementation class. In order to do that, we can use the Header's operationName parameter to determine which method we are going to invoke:

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
       http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
       http://camel.apache.org/schema/blueprint/cxf http://camel.apache.org/schema/blueprint/cxf/camel-cxf.xsd
       http://camel.apache.org/schema/blueprint http://camel.apache.org/schema/blueprint/camel-blueprint.xsd">

  <cxf:cxfEndpoint id="customerEndpoint" address="http://localhost:9001/customerService" serviceClass="com.mycompany.service.CustomerService" />
  <bean id="beanProcessor" class="com.mycompany.processor.BeanProcessor" />
  <camelContext trace="false" id="blueprintContext" xmlns="http://camel.apache.org/schema/blueprint">
    <route id="cxfRoute">
        <from uri="cxf:bean:customerEndpoint"/>
        <log message="${header.operationName}"/>
    <route id="findCustomer">
        <from uri="direct:findCustomer"/>
        <bean method="findCustomer" ref="beanProcessor"/>
    <route id="deleteCustomer">
        <from uri="direct:deleteCustomer"/>
        <bean method="deleteCustomer" ref="beanProcessor"/>


In this example, the request are routed to the BeanProcessor class which contains a simple logic to return a Customer or a String as result of the two actions available:

package com.mycompany.processor;

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

import com.mycompany.service.Customer;

public class BeanProcessor {
	HashMap<String, Customer> map = new HashMap();

	public BeanProcessor() {

		Customer c = new Customer();
		c.setAddress("NY City");

		Customer c2 = new Customer();
		c2.setAddress("Los Angeles");

		map.put("1", c);
		map.put("2", c2);


	public Customer findCustomer(String cId) throws Exception {

		return map.get(cId);


	public String deleteCustomer(String cId) throws Exception {

		Object obj = map.get(cId);
		if (obj == null) {
			return "not Found!";
		} else {
			return "Deleted!";



 Now you can test invoking the Web service using SOAP UI or any other Web service tester:

camel apache cxf tutorial



Cookie Alert