Home

Apache Camel Rest Webservice using CXFRS Sample

Introduction
In this article we will expose a REST Webservice using Apache Camel and CXFRS. Using JAX-RS we can configure the server to expose a REST services which returns an output by directly calling the specified resource class. If we have to use the camel route for returning the response then we make use of CXF-RS.
Overview
We will be creating a Maven project with minimum dependencies added to POM for getting Camel up and running. In future chapter’s further dependencies will be added to the POM.xml as and when required. The dependencies are added for deploying our application on JBoss Fuse.
Step 1: In eclipse Go to File->other->Maven Project. Click Next Button.
Step 2:  If not already selected, select Use default Location, select a simple project. Click Next Button.
Step 3:  Enter the values for Group Id as com.eai.integration and Artifact Id as Integration-API-Sample and click on finish.
Our Maven Web Project is now created. 
Add dependencies required for Apache Camel- Here we have only added the camel-core dependency. After modification our pom.xml file is as follows.

    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.eai.integration</groupId>

    <artifactId>Integration-API-Sample</artifactId>

    <version>0.0.1-SNAPSHOT</version>

    <name>Integration-API-Sample</name>

    <description>Integration-API-Sample</description>



    <dependencies>

        <dependency>

            <groupId>org.apache.camel</groupId>

            <artifactId>camel-core</artifactId>

            <version>2.20.0</version>

        </dependency>

        <dependency>

            <groupId>org.apache.camel</groupId>

            <artifactId>camel-cxf</artifactId>

            <version>2.20.0</version>

        </dependency>

    </dependencies>

    <build>

        <plugins>

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-compiler-plugin</artifactId>

                <version>3.1</version>

            </plugin>



            <plugin>

                <groupId>org.apache.felix</groupId>

                <artifactId>maven-bundle-plugin</artifactId>

                <extensions>true</extensions>

                <version>2.4.0</version>

            </plugin>

        </plugins>

    </build>

</project>
Create the Resource class name as SampleResource Note here that we are not going to use this class to return the response. The controller method here is returning null.
package com.eai.integration;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

/**
 * @author Ameer
 *
 */
@Path("/v1.0")
public class SampleResource {
    @GET
    @Path("/sample/{name}/")
    public String getGreetingMessage(@PathParam("name") String name) {
        return null;
    }
}
Create the Camel Processor class name as ‘SampleProcessor’. Using the cxfrs endpoint we will route the message to this Processor class.
package com.eai.integration.processor;

import org.apache.camel.Exchange;

/**
 * @author Ameer
 *
 */
public class SampleProcessor{
    
    public void process(Exchange exchange) throws Exception {

        // Get input from exchange
        String name = exchange.getIn().getBody(String.class);
        // set output in exchange
        exchange.getOut().setBody("Hello, " + name);
    }
}
Under the resources folder create Folders->META-INF->spring. This is the default path where Fuse looks for the config files. Create a camelContext.xml and we will configure the Rest Server using CXFRS. Also we use the cxfrs endpoint to route the incoming message to the Processor.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://camel.apache.org/schema/cxf"
    xmlns:jaxrs="http://cxf.apache.org/jaxrs"
    xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf.xsd
       http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

    <cxf:rsServer address="http://localhost:9080/api" id="RestEndpoint">
        <cxf:serviceBeans>
            <ref bean="serviceClass" />
        </cxf:serviceBeans>
    </cxf:rsServer>

    <bean id="serviceClass" class="com.eai.integration.SampleResource" />
    <bean id="sampleProcessor" class="com.eai.integration.processor.SampleProcessor" />
    
    <camelContext id="sampleCamelContext" xmlns="http://camel.apache.org/schema/spring">
        <route id="sampleRoute">
            <from uri="cxfrs://bean://RestEndpoint" />
            <bean ref="sampleProcessor" method="process"/>
        </route>
    </camelContext>
</beans>
Right click on Integration-API-Sample à Run As à Maven install. This will build the application and then deploy it in JBoss Fuse as follows.
osgi:install -s mvn:com.eai.integration/Integration-API-Sample/0.0.1-SNAPSHOT
Now that the bundle has started, if we go to http://localhost:8181/cxf we see that our REST service is shown as successfully deployed. We can also get the WADL URL from here.
Since it’s a simple GET request we will use the browser to test it. In the browser go to the url.
Conclusion
We just learnt how to implement a fuse ESB project using Apache camel. Here we have implemented simple Rest API which is returning Hello, <name>. We used CXFRS for implementing this rest API. We will learn more related to apache camel, fuse ESB and Rest API in future articles. 
Downloads
S. No
File Name
Size
Download
1
Apache Camel Rest Webservice using CXFRS Sample.pdf
1 MB
Download
2
Integration-API-Sample.zip
16 KB
Download

Comments