In this example the Camel CXFRS component is used to expose a REST interface to a database table.apache_feather

The database table we’re using is quite simple. It has an auto incrementing ID, a company name and a stock symbol. I used Derby for my database (though any JDBC database would work) to keep it simple. Since the example uses an in memory database it needs to be set  up  each time the example is run. does this. The datasource field is injected by Spring  and the method create() creates the DB.

In the Spring camel context file create the datasource and an instance of DatabaseBean which calls create().

Now whenever the example is run an in memory instance of Derby is created and the table company is created.

CXFRS requires that you create a resource class marked up using annotation. In the case of the camel-cxfrs component this class isn’t actually called so we can use an interface instead.

Now the only thing left is to define the routes.

First I create the CXFRS consumer endpoint. I pass in the Resource class used to define the valid operations. When the address is hit CXFRS looks for any matching operations and if it finds one it sets the header operationName to the name of the method associated with that address. Otherwise it returns a 404.  Then I use .choice() to determine which operation was selected and send the appropriate SQL to the sql endpoint. Finally, the result is marshaled to JSON and returned to the caller.

As always, the full example is at GitHub.

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

Comments on this entry are closed.

  • Srinivasa Reddy Challa

    can you also please let me know how do i run the example?

    • tjsnell

      You can run the code using: mvn camel:run

  • laskan

    how to deploy on server?

  • Sayed Abdul Islam

    Unable to test the POST method, always inserts Null value.

    Note: Have done a little change to service as thrown error:

    public String createCompany(@PathParam(“name”) String name,@PathParam(“symbol”) String symbol);

    Please share a java client to test it.

    Best regards,