Search Tutorials


Apache Camel + Rest Webservice using CXF-RS Hello World Example | JavaInUse

Apache Camel + Rest Webservice using CXFRS Hello World Example

In this post we will expose a REST Webservice using Apache Camel and CXFRS. In a previous post we had implemented REST Webservice using Apache Camel and JAX-RS CXFRS is indeed just one of them but as you can guess from its name it is dedicated to supporting HTTP endpoints and clients written on top of Apache CXF JAX-RS implementation.
Using JAX-RS we can configure the server to expose a REST serverice 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.

camel-27_5

Apache Camel - Table of Contents

File Transfer Using Java DSL Apache Camel Apache Camel Java DSL + Spring Integration Hello World Example Apache Camel Exception Handling Using Simple Example Apache Camel Redelivery policy using example Integrate Apache Camel and ActiveMQ EIP patterns using Apache Camel Apache Camel Tutorial- Integrate Spring Boot+ Apache Camel Apache Camel Tutorial- Integrate with MySQL DB using SQL query Apache Camel EIP - Splitter and Aggregator pattern Apache Camel Unit Testing Apache Camel + Spring + Quartz Hello World Example Camel application deployment on JBoss Fuse Apache Camel + Apache CXF SOAP Webservices Apache Camel + JAX-RS REST Webservice Apache Camel + CXFRS REST Webservice Apache Camel Routing Slip EIP Pattern Apache Camel Dynamic Router Pattern Apache Camel Load Balancer EIP Pattern Apache Camel Interceptors Apache Camel + Kafka Hello World Example Apache Camel - Marshalling/Unmarshalling XML/JSON Data Example Calling and Consuming Webservices using Apache Camel Apache Camel Tutorial - Send SMTP Email Using Gmail Apache Camel Tutorial - SEDA component Hello World Example Spring Boot + Apache Camel + RabbitMQ - Hello World Example Apache Camel Tutorial - Idempotent Consumer using MemoryIdempotentRepository and FileIdempotentRepository Spring Boot + Apache Camel JDBC component + MySQL - Hello World Example Spring Boot + Apache Camel SQL component + MySQL - Hello World Example Spring Boot + Apache Camel SQL component + Transaction Management Example

Video

This tutorial is explained in the below Youtube Video.

Lets Begin

The project structure we would be creating will be as follows-

camel-27_4

The pom.xml will be as follows-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	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.javainuse</groupId>
	<artifactId>apache-camel-cxfrs</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<packaging>bundle</packaging>

	<dependencies>
		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-core</artifactId>
			<version>2.12.0</version>
		</dependency>
		<dependency>
			<groupId>org.apache.camel</groupId>
			<artifactId>camel-cxf</artifactId>
			<version>2.12.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 as follows. Note here that we are not going to use this class to return the response. The controller method here is returning null.
package com.javainuse.beans;

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

@Path("/")
public class EmployeeServiceResource {

	public EmployeeServiceResource() {
	}

	@GET
	@Path("/employees/{name}/")
	public String getCustomer(@PathParam("name") String name) {
		return null;
	}

}

Create the Camel Processor class as follows. Using the cxfrs endpoint we will route the message to this Processor class
package com.javainuse.beans;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;

public class CamelProcessor implements Processor {

	public void process(Exchange exchange) throws Exception {

		// Get input from exchange
		String msg = exchange.getIn().getBody(String.class);
		// set output in exchange
		exchange.getOut().setBody("Hello World " + msg);
	}

}

Next in the applicationContext.xml we 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 id="restService" address="http://localhost:9000/employeeservice"
		serviceClass="com.javainuse.beans.EmployeeServiceResource">
	</cxf:rsServer>

	<bean id="processor" class="com.javainuse.beans.CamelProcessor" />

	<camelContext id="camelId" xmlns="http://camel.apache.org/schema/spring">
		<route>
			<from uri="cxfrs://bean://restService" />
			<process ref="processor" />
		</route>
	</camelContext>

</beans>
Next we build the application and then deploy it in JBoss Fuse as follows-
camel-27_1
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.
camel-27_2
Since its a simple GET request we will use the browser to test it. In the browser go to the url.
camel-27_3

Download Source Code

Download it - Apache Camel + Rest Webservice using CXFRS Integration simple example

See Also

Spring Boot Hello World Application- Create simple controller and jsp view using Maven Spring Boot Tutorial-Spring Data JPA Spring Boot + Simple Security Configuration Pagination using Spring Boot Simple Example Spring Boot + ActiveMQ Hello world Example Spring Boot + Swagger Example Hello World Example Spring Boot + Swagger- Understanding the various Swagger Annotations Spring Boot Main Menu Spring Boot Interview Questions