4.5. Swagger Integration

download PDF


Integrated with the REST DSL, the camel-swagger component enables users to create API docs for any REST-defined routes and endpoints in a CamelContext file. The camel-swagger component creates a servlet integrated with the CamelContext that pulls the information from each REST endpoint to generate the API docs (JSON file).
If using Maven, you need to add the camel-swagger component to your pom.xml file:
   <!-- Use the same version as your Camel core version --> 

Configuring the camelContext

To enable Swagger, add to the CamelContext file:
  • The service element, which exposes the camel-swagger servlet and initializes its parameters.
    In the service element, add the servlet (org.apache.camel.component.swagger.DefaultCamelSwaggerServlet) and the service-properties that configure the servlet's parameters.
    For details on servlet parameters, see chapter "Swagger" in "Apache Camel Component Reference".
  • Configure the REST implementation
    Define the REST service within the camelContext element using the restConfiguration and rest elements.
    For details on configuring REST services in the CamelContext, see Section 4.2, “Defining Services with REST DSL”.
For OSGi deployments, you need to configure the servlet options
 and REST configuration in the blueprint.xml file; for example:
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns=""

    <service interface="javax.servlet.http.HttpServlet">
            <entry key="alias" value="/api-docs/*"/>
            <entry key="init-prefix" value="init."/>
            <entry key="init.base.path" value="//localhost:8080/"/>
            <entry key="init.api.path" value="//localhost:8181/api-docs"/>
            <entry key="init.api.title" value="Camel Rest Example API"/>
            <entry key="init.api.version" value="1.2"/>
            <entry key="init.api.description"
                value="Camel Rest Example with Swagger that provides an User REST service"/>
        <bean class="org.apache.camel.component.swagger.DefaultCamelSwaggerServlet" />

    <camelContext id="log-example-context"

        <restConfiguration component="jetty" port="8080"/>
        <rest path="/say">
            <get uri="/hello">
                <to uri="direct:hello"/>
            <get uri="/bye" consumes="application/json">
                <to uri="direct:bye"/>
            <post uri="/bye">
                <to uri="mock:update"/>
        <route id="rte1-log-example">
            <from uri="direct:hello"/>
                <constant>Hello World</constant>
        <route id="rte2-log-example">
            <from uri="direct:bye"/>
                <constant>Bye World</constant>


The following parameters are used in the above example as described here:
The service element exposes the camel swagger servlet (<bean class="org.apache.camel.component.swagger.DefaultCamelSwaggerServlet"/>) and initializes several servlet properties.
The alias property binds the camel swagger servlet to /api-docs/*.
The init-prefix property sets the prefix for all camel swagger servlet properties to init.. This is analogous to using init-param elements in the web.xml configuration for WAR implementations (see chapter "Swagger" in "Apache Camel Component Reference").
In the camelContext element, the restConfiguration element specifies the REST implementation to use. In this case, it is Jetty web servlet on port 8080.
In the camelContext element, the rest element defines a REST service and provides the base path (/say) to it. In this case, the service consists of two REST endpoints, hello and bye, which are routed to their corresponding camel endpoints defined in the route elements.
Red Hat logoGithubRedditYoutubeTwitter


Try, buy, & sell


About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

© 2024 Red Hat, Inc.