Spring Boot, Apache Camel and Swagger UI

3 min read

Companies are using lots of systems which have to talk with each other. Therefore it's a good idea to use something to integrate these systems. Such an application integration can be done by an Enterprise Service Bus or even with an integration framework like Apache Camel. I highly recommend to use one of these and don't program it on your own by using lots of APIs. Please don't do this!
I'm a big fan of lightweight integration and flexibility so it's not surprising that Apache Camel is my tool of choice. Apache Camel can be used in standalone mode by using a jar containing the Camel routes or as part of a runtime like Spring Boot or WildFly/WildFly Swarm for example. I decided to use Spring Boot as runtime because it is widely used and comes with a lot of useful things, a very good documentation and so on. In addition to that I'm usind Swagger UI to get a beautiful API documentation which makes consuming REST APIs much easier.
This blog posts describes how to combine these technologies with each other. In case you just want to use the code, here's the link to my GitHub repository.

Create a Spring Boot and Apache Camel project

First we have to create a Spring Boot project containing the Apache Camel dependency first. This can easily be done by using the Spring Initializr which is available at: https://start.spring.io/

You just change the Group and Artifact to the names you like and Add Apache Camel as Spring Boot Starters dependency. After that you have to click on Generate Project and you project is up and running.

Integrate Swagger UI in Spring Boot

Now we can add the Swagger UI files to our Spring Boot project. This can be done by downloading the Swagger UI sources from: https://github.com/swagger-api/swagger-ui.

Note: I'm using Swagger UI 2.2.8 instead of the latest version because the Camel component doesn't support the new API specification of Swagger 3.X for now.

After that we have to create a new folder at src/main/resources which have to be named: static, public or resources to serve our static Swagger UI content via Spring Boot. Now we simply have to add the dist folder of Swagger UI to this folder to make Swagger UI available in our project.


After that Swagger UI is available at: http://localhost:8080/swagger/index.html

Add a Camel route

In the next step we have to add a simple Camel route to the Spring Boot project. By doing this we have to add a Camel servlet first. This snippet adds a servlet mapping which will be the prefix of all Camel routes we will add later. After that all camel routes will be available at localhost:8080/api/*. Now its time to add the route itself: This route adds a person REST URI which can be used by HTTP GET only and returns a JSON response of my Person POJO class: After these steps the Person respone is available at: http://localhost:8080/api/person

Combining Swagger and Camel routes

After adding the REST API it's time to combine Camel and Swagger UI with each other. This can be done by changing the url in the swagger index.html from the sample petstore to /api-doc/camel-1: Now we have to add a servlet mapping for Swagger to use the Camel route urls: After that Swagger UI will use the Camel REST API instead of the sample petstore. It's important to change the base.path of the servlet to api because our REST APIs have /api/ as prefix. These steps let Swagger UI show a beautiful documentation of our Camel REST APIs.

Have fun with it! :-)

Bye,
Bennet


Be Social, Share!