Easy REST endpoints with Apache Camel 2.14
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?
k.. 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") .consumes("application/json").produces("application/json") .get("/{id}").description("Find user by id").outType(User.class) .to("bean:userService?method=getUser(${header.id})") .put().description("Updates or create a user").type(User.class) .to("bean:userService?method=updateUser") .get("/findAll").description("Find all users").outTypeList(User.class) .to("bean:userService?method=listUsers");
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:
restConfiguration().component("jetty") .bindingMode(RestBindingMode.json) .dataFormatProperty("prettyPrint", "true") .port(8080);
Or this in your Spring context XML:
<camelContext> ... <restConfiguration bindingMode="auto" component="jetty" port="8080"/> ... </camelContext>
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.
Reference: | Easy REST endpoints with Apache Camel 2.14 from our JCG partner Christian Posta at the Christian Posta – Software Blog blog. |