Exposing REST Services with Camel

In this tutorial we will learn how to create a simple Camel REST Consumer from a Java interface.

So your starting point, will be defining an interface containing the REST operations which will be exposed by Camel:

package com.mycompany.service;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/customer")
public interface CustomerServiceREST {

	@GET
	@Path("/findCustomer/{id}")
	@Produces(MediaType.APPLICATION_JSON)
	public Customer findCustomer(@PathParam("id") String id);

	@GET
	@Path("/deleteCustomer/{id}")
	public String deleteCustomer(@PathParam("id") String id);
}

Exposing this interface through Camel can be done through the CXF Component which will define a REST endpoint bound to the service class we have just introduced:

<cxf:rsServer id="restEndpoint" address="http://localhost:9001/rest" serviceClass="com.mycompany.service.CustomerServiceREST" />

The implementation of the Service will be contained in a Processor which contains the two methods findCustomer and deleteCustomer:

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!";
		}

	}

}

Here is the blueprint.xml which contains the Camel route used to match the incoming REST requests with the Processor's methods:

<?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:rsServer id="restEndpoint" address="http://localhost:9001/rest"
		serviceClass="com.mycompany.service.CustomerServiceREST" />

	<bean id="beanProcessor" class="com.mycompany.processor.BeanProcessor" />

	<camelContext trace="false" id="blueprintContext"
		xmlns="http://camel.apache.org/schema/blueprint">

		<route>
			<from uri="cxfrs:bean:restEndpoint" />
			<log message="${header.operationName}" />
			<choice>
				<when>
					<simple>${header.operationName} == "findCustomer"</simple>
					<bean ref="beanProcessor" method="findCustomer" />
				</when>
				<when>
					<simple>${header.operationName} == "deleteCustomer"</simple>
					<bean ref="beanProcessor" method="deleteCustomer" />
				</when>
			</choice>
			<marshal>
				<json prettyPrint="true" library="Jackson" />
			</marshal>
		</route>
	</camelContext>

</blueprint>

Now you can test the two REST services with simple GET requests:

camel rest tutorial

camel rest tutorial

 

 


Advertisement

Cookie Alert