Tuesday, November 19, 2013

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:

 contextConfigLocation
 classpath:camel-config.xml
    
    
       org.springframework.web.context.ContextLoaderListener
 
    
and map the Restlet servlet

  RestletServlet
  org.restlet.ext.spring.SpringServerServlet
  
    org.restlet.component
    RestletComponent
  


  RestletServlet
  /rs/*
In the Spring context, there is a little more Restlet and an in-memory datasource setup code:
     
    
       
         
       
    
    
       
     
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:

   
   
     insert into user(firstName, lastName) values('${header.firstName}','${header.lastName}');  
   
   
   
     select * from user ORDER BY id desc LIMIT 1
   
   
The ‘manipulateUser’ route handles GET, PUT and DELETE HTTP methods, but depending on the method used, it executes different SQL commands:

  
  
    
    ${header.CamelHttpMethod} == 'GET'
    
      select * from user where id = ${header.userId}
    
   
   
     ${header.CamelHttpMethod} == 'PUT'
       
       update user set firstName='${header.firstName}', lastName='${header.lastName}' where id = ${header.userId}
       
   
   
     ${header.CamelHttpMethod} == 'DELETE'
     
       delete from user where id = ${header.userId}
     
   
   
     
   
  
  
And the last route for listing all the users is self explanatory:

  
  
    select * from user
  
  
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.

5 comments:

oakleyses said...

louis vuitton handbags, oakley sunglasses, louboutin, longchamp outlet, nike shoes, louis vuitton outlet stores, chanel handbags, burberry outlet, prada outlet, jordan shoes, tiffany and co, michael kors outlet, tory burch outlet, louis vuitton outlet, longchamp handbags, nike free, true religion jeans, michael kors outlet, kate spade outlet, polo ralph lauren outlet, tiffany and co, prada handbags, polo ralph lauren outlet, michael kors outlet, michael kors outlet, longchamp handbags, oakley sunglasses, ray ban sunglasses, kate spade handbags, burberry outlet, louis vuitton outlet, louboutin outlet, louboutin, coach factory outlet, air max, air max, coach outlet, gucci outlet, christian louboutin shoes, michael kors outlet, coach purses, ray ban sunglasses, michael kors outlet, louis vuitton, coach outlet store online, true religion jeans, oakley sunglasses cheap

oakleyses said...

ralph lauren, lululemon, air max, hollister, north face, nike air max, polo lacoste, vanessa bruno, timberland, vans pas cher, louboutin, louis vuitton, oakley pas cher, air max pas cher, nike roshe run, air max, true religion outlet, barbour, sac longchamp, air force, hollister, sac louis vuitton, nike free, polo ralph lauren, nike trainers, louis vuitton uk, nike roshe, sac hermes, longchamp, michael kors, sac burberry, sac guess, mulberry, new balance pas cher, converse pas cher, sac louis vuitton, hogan outlet, nike tn, north face, true religion outlet, ray ban pas cher, michael kors, air jordan, nike blazer, nike free pas cher, michael kors pas cher, abercrombie and fitch, ray ban sunglasses

oakleyses said...

mac cosmetics, mont blanc, marc jacobs, canada goose outlet, nike huarache, vans shoes, soccer jerseys, hollister, giuseppe zanotti, beats by dre, abercrombie and fitch, longchamp, insanity workout, celine handbags, bottega veneta, ghd, nfl jerseys, north face outlet, chi flat iron, ugg boots, birkin bag, ugg australia, canada goose, herve leger, ugg pas cher, rolex watches, valentino shoes, canada goose uk, canada goose, ferragamo shoes, canada goose, ugg boots, uggs outlet, north face jackets, soccer shoes, asics running shoes, new balance shoes, p90x, lululemon outlet, canada goose jackets, mcm handbags, instyler, babyliss pro, ugg, wedding dresses, jimmy choo outlet, reebok outlet, nike roshe run

oakleyses said...

cparajumpers, karen millen, air max, converse, pandora charms, moncler, louboutin, moncler, links of london, lancel, juicy couture outlet, oakley, hollister, pandora charms, supra shoes, thomas sabo, canada goose, gucci, wedding dresses, timberland boots, swarovski crystal, air max, coach outlet store online, moncler, ray ban, canada goose, moncler, ugg, louis vuitton, swarovski, hollister, montre homme, moncler, hollister clothing store, ralph lauren, rolex watches, moncler outlet, moncler, iphone 6 cases, baseball bats, juicy couture outlet, toms shoes, vans, pandora jewelry, ugg, converse shoes

Anna said...

Great and Useful Article.

Online Java Course

Java Online Training

Java Course Online

Best Recommended books for Spring framework

Java Interview Questions












Java Training Institutes in Chennai

Java Training in Chennai

J2EE Training in Chennai

java j2ee training institutes in chennai

Java Course in Chennai