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"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:camel="http://camel.apache.org/schema/blueprint"
       xmlns:cxf="http://camel.apache.org/schema/blueprint/cxf"
       xsi:schemaLocation="
       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}"/>
        <recipientList>
            <simple>direct:${header.operationName}</simple>
        </recipientList>
    </route>
    <route id="findCustomer">
        <from uri="direct:findCustomer"/>
        <bean method="findCustomer" ref="beanProcessor"/>
    </route>
    <route id="deleteCustomer">
        <from uri="direct:deleteCustomer"/>
        <bean method="deleteCustomer" ref="beanProcessor"/>
    </route>
    
  
</camelContext>

</blueprint>

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.setId("1");
		c.setName("John");
		c.setSurname("Doe");
		c.setAddress("NY City");

		Customer c2 = new Customer();
		c2.setId("2");
		c2.setName("Mark");
		c2.setSurname("Twain");
		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 {
			map.remove(cId);
			return "Deleted!";
		}

	}

}

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

camel apache cxf tutorial

 


Advertisement

Cookie Alert