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:


<beans>
<jaxrs:server address="camel://direct:bookStore"
serviceClass="service.BookStoreImpl"/>

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

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

<!-- Jetty to CXF JAX-RS -->
<route>
<from
uri="jetty:http://0.0.0.0:9002/bookstore?matchOnUriPrefix=true"/>
<to uri="direct:bookStore"/>
</route>
</camelContext>
</beans>


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.