Tuesday, May 10, 2011

How to use Camel transport with CXF JAX-RS endpoints

The following configuration shows how to have HTTP or JMS messages forwarded to
CXF JAX-RS endpoints in Camel:

<jaxrs:server address="camel://direct:bookStore"

<camelContext xmlns="http://camel.apache.org/schema/spring">

<!-- JMS to CXF JAX-RS -->
<from uri="jms://test.bookStore"/>
<to uri="direct:bookStore"/>

<!-- Jetty to CXF JAX-RS -->
<to uri="direct:bookStore"/>

Note that you need to use "camel://direct:bookStore" as the address of the jaxrs endpoint and then use "direct:bookStore" in route definitions.

The above configuration will only work with the current CXF trunk and 2.3.x branch snapshots.
The address like "camel://direct:bookStore" is confusing CXF JAX-RS a bit in 2.3.4/2.4.0 and earlier.

However a workaround is available for earlier versions. Christian wrote a couple of Talend Integration Factory demos showing the workaround in action. It is just a matter of adding a simple handler into routes which will make sure CXF JAX-RS is capable of matching requests forwarded to "camel://direct:bookStore".

That said, I'd encourage those who are interested to check Christian's demos anyway, for example, the jaxrs-http-jms demo also shows how to build a Camel route which gets the response from the jaxrs endpoint and converts it using a JAXB converter and pushes it further to the JMS queue.