About Bilgin Ibryam

Bilgin Ibryam is a senior software engineer based in London interested in service-oriented architecture, enterprise application integration and application development. He is also open source enthusiast, Apache Open for Business and Apache Camel committer.

REST with Apache Camel

There are many ways to expose an HTTP endpoint in Camel: jetty, tomcat, servlet, cxfrs and restlet. Two of these components – cxfrs and restlet also support REST semantics just with few lines of code. This simple example demonstrates how to do CRUD operations with camel-restlet and camel-jdbc. The four HTTP verbs execute different operations and map to the following single URI template:

  • POST – create a new user: /user
  • GET – request the current state of the user specified by the URI: /user/{userId}
  • PUT – update an user at the given URI with new information: /user/{userId}
  • DELETE – remove the user identified by the given URI: /user/{userId}

There is also a /users URI which returns all the users regardless of the HTTP method used. Creating such an application with Camel is straightforward. After adding all the necessary dependencies (restlet, spring, jdbc…) configure web.xml to load Camel context:


and map the Restlet servlet


In the Spring context, there is a little more Restlet and an in-memory datasource setup code:

     <bean id="RestletComponent" class="org.restlet.Component"/>
    <bean id="RestletComponentService" class="org.apache.camel.component.restlet.RestletComponent">
       <constructor-arg index="0">
         <ref bean="RestletComponent"/>
    <jdbc:embedded-database id="dataSource" type="HSQL">
       <jdbc:script location="classpath:sql/init.sql"/>

After all the setup is done, the next step is to create Camel routes that will process the HTTP requests and execute appropriate CRUD operations. The first one is createUser route that executes SQL insert command with the parameters from POST requests only and return the newly created user in the response body:

<route id="createUser">
   <from uri="restlet:/user?restletMethod=POST"/>
     <simple>insert into user(firstName, lastName) values('${header.firstName}','${header.lastName}');  </simple>
   <to uri="jdbc:dataSource"/>
     <simple>select * from user ORDER BY id desc LIMIT 1</simple>
   <to uri="jdbc:dataSource"/>

The ‘manipulateUser’ route handles GET, PUT and DELETE HTTP methods, but depending on the method used, it executes different SQL commands:

<route id="manipulateUser">
  <from uri="restlet:/user/{userId}?restletMethods=GET,PUT,DELETE"/>
    <simple>${header.CamelHttpMethod} == 'GET'</simple>
      <simple>select * from user where id = ${header.userId}</simple>
     <simple>${header.CamelHttpMethod} == 'PUT'</simple>
       <simple>update user set firstName='${header.firstName}', lastName='${header.lastName}' where id = ${header.userId}</simple>
     <simple>${header.CamelHttpMethod} == 'DELETE'</simple>
       <simple>delete from user where id = ${header.userId}</simple>
  <to uri="jdbc:dataSource"/>

And the last route for listing all the users is self explanatory:

<route id="listUsers">
  <from uri="restlet:/users"/>
    <constant>select * from user</constant>
  <to uri="jdbc:dataSource"/>

If you want to see the application in action, grab the source code from github and run it with the embedded maven-jetty plugin by typing: mvn jetty:run .You can even try some quick queries if you have curl installed:

To create an user, make a http POST request with firstName and lastName parameters

curl -d 'firstName=test&lastName=user' http://localhost:8080/rs/user/

To update an existing user, make a http PUT request with firstName and lastName parameters

curl -X PUT -d 'firstName=updated&lastName=user' http://localhost:8080/rs/user/2

To retrieve an existing user, make a http GET request with the userId as part of the url

curl -X GET http://localhost:8080/rs/user/2

To delete an existing user, make a http DELETE request with the userId as part of the url

curl -X DELETE http://localhost:8080/rs/user/2

To retrieve all the existing users, make a http GET request to users url

curl -X GET http://localhost:8080/rs/users


Reference: REST with Apache Camel from our JCG partner Bilgin Ibryam at the OFBIZian blog.

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply

six × = 12

Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books