Over a million developers have joined DZone.

Easy REST Endpoints with Apache Camel 2.14

DZone 's Guide to

Easy REST Endpoints with Apache Camel 2.14

· Integration Zone ·
Free Resource

Apache Camel has a new release recently, and some of the new features were blogged about by my colleague Claus Ibsen. You really should check out his blog entry and dig into more detail, but one of the features I was looking forward to trying was the new REST DSL.

So what is this new DSL?

Actually, it’s an extension to Camel’s routing DSL, which is a powerful domain language for declaratively describing integration flows and is available in many flavors. It’s pretty awesome, and is a differentiator between integration libraries. If you haven’t seen Camel’s DSL, you should check it out. Have I mentioned that Camel’s DSL is awesome?

All right... back to the REST story here...

Before release 2.14, creating rest endpoints meant using camel-cxfrs which can be difficult to approach for a new user just trying to expose a simple REST endpoint. Actually, it’s a very powerful approach to doing contract-first REST design, but I’ll leave that for the next blog post. However, in a previous post I did dive into using camel-cxfrs for REST endpoints so you can check it out.

With the 2.14, the DSL has been extended to make it easier to create REST endpoints. For example:

        rest("/user").description("User rest service")

            .get("/{id}").description("Find user by id").outType(User.class)

            .put().description("Updates or create a user").type(User.class)

            .get("/findAll").description("Find all users").outTypeList(User.class)

In this example, we can see we use the DSL to define REST endpoints, and it’s clear, intuitive and straight forward.

All you have to do is set up the REST engine with this line:

            .dataFormatProperty("prettyPrint", "true")

Or this in your Spring context XML:

  <restConfiguration bindingMode="auto" component="jetty" port="8080"/>  

The cool part is you can use multiple HTTP/servlet engines with this approach, including a micrservices style with embedded jetty (camel-jetty) or through an existing servlet container (camel-servlet). Take a look at the REST DSL documentation for the complete of HTTP/servlet components you can use with this DSL.

Lastly, some might ask, what about documenting the REST endpoint? Eg, WADL?
Well, luckily, the new REST DSL is integrated out of the box with awesome Swagger library and REST documenting engine ! So you can auto document your REST endpoints and have the docs/interface/spec generated for you! Take a look at the camel-swagger documentation and the camel-example-servlet-rest-tomcat example that comes with the distribution to see more.

Give it a try, and let us know (Camel mailing list, comments, stackoverflow, somehow!!!) how it works for you.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}