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:

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:

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

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial3>

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

3 comments

Hi Bilgin, Thanks for your wonderful blog – I am actually using your example to become familiar with Camel-Restlet. I am new to Camel and am faced with an issue while deploying this code to Tomcat – I get the following exception – any idea on what needs to be done here?

Yes Bilgin, I have added that…the issue was with the incorrect version of the dependency org.restlet.jee:org.restlet.ext.spring. I was using restlet 2.11.1 and should have gone with org.restlet.jee:org.restlet.ext.spring version 2.0.15 – instead I was using version 2.2.1. Once I changed this version to 2.0.15, it worked without any issues. Thanks for your response Bilgin.

Newsletter

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.

Email address:

Join Us

With 1,043,221 monthly unique visitors and over 500 authors we are placed among the top Java related sites around. Constantly being on the lookout for partners; we encourage you to join us. So If you have a blog with unique and interesting content then you should check out our JCG partners program. You can also be a guest writer for Java Code Geeks and hone your writing skills!

Disclaimer

All trademarks and registered trademarks appearing on Examples 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. Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.