Openapi Java

Since Camel 3.1

The Rest DSL can be integrated with the camel-openapi-java module which is used for exposing the REST services and their APIs using OpenApi.

Maven users will need to add the following dependency to their pom.xml for this component:

    <!-- use the same version as your Camel core version -->

The camel-openapi-java module can be used from the REST components (without the need for servlet)

Using OpenApi in rest-dsl

You can enable the OpenApi api from the rest-dsl by configuring the apiContextPath dsl as shown below:

public class UserRouteBuilder extends RouteBuilder {
    public void configure() throws Exception {
        // configure we want to use servlet as the component for the rest DSL
        // and we enable json binding mode
            // and output using pretty print
            .dataFormatProperty("prettyPrint", "true")
            // setup context path and port number that netty will use
            // add OpenApi api-doc out of the box
                .apiProperty("api.title", "User API").apiProperty("api.version", "1.2.3")
                // and enable CORS
                .apiProperty("cors", "true");

        // this user REST service is json only
        rest("/user").description("User rest service")
            .get("/{id}").description("Find user by id").outType(User.class)
                .param().name("id").type(path).description("The id of the user to get").dataType("int").endParam()
            .put().description("Updates or create a user").type(User.class)
                .param().name("body").type(body).description("The user to update or create").endParam()
            .get("/findAll").description("Find all users").outType(User[].class)


The OpenApi module can be configured using the following options. To configure using a servlet you use the init-param as shown above. When configuring directly in the rest-dsl, you use the appropriate method, such as enableCORS, host,contextPath, dsl. The options with is configured using apiProperty dsl.

Option Type Description



Whether to enable CORS. Notice this only enables CORS for the api browser, and not the actual access to the REST services. Is default false.



OpenApi spec version. Is default 3.0.



To setup the hostname. If not configured camel-openapi-java will calculate the name as localhost based.



The protocol schemes to use. Multiple values can be separated by comma such as "http,https". The default value is "http".



Required: To setup the base path where the REST services is available. The path is relative (eg do not start with http/https) and camel-openapi-java will calculate the absolute base path at runtime, which will be protocol://host:port/context-path/base.path



To setup the path where the API is available (eg /api-docs). The path is relative (eg do not start with http/https) and camel-openapi-java will calculate the absolute base path at runtime, which will be protocol://host:port/context-path/api.path So using relative paths is much easier. See above for an example.



The version of the api. Is default 0.0.0.



The title of the application.



A short description of the application.



A URL to the Terms of Service of the API.


Name of person or organization to contact


An email to be used for API-related correspondence.


A URL to a website for more contact information.


The license name used for the API.



A URL to the license used for the API.

Adding Security Definitions in API doc

The Rest DSL now supports declaring OpenApi securityDefinitions in the generated API document. For example as shown below:

rest("/user").tag("dude").description("User rest service")
    // setup security definitions

Here we have setup two security definitions

  • OAuth2 - with implicit authorization with the provided url

  • Api Key - using an api key that comes from HTTP header named myHeader

Then you need to specify on the rest operations which security to use by referring to their key (petstore_auth or api_key).

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


.put().description("Updates or create a user").type(User.class)
    .security("petstore_auth", "write:pets,read:pets")

Here the get operation is using the Api Key security and the put operation is using OAuth security with permitted scopes of read and write pets.

JSon or Yaml

The camel-openapi-java module supports both JSon and Yaml out of the box. You can specify in the request url what you want returned by using /openapi.json or /openapi.yaml for either one. If none is specified then the HTTP Accept header is used to detect if json or yaml can be accepted. If either both is accepted or none was set as accepted then json is returned as the default format.

useXForwardHeaders and API URL resolution

The OpenApi specification allows you to specify the host, port & path that is serving the API. In OpenApi V2 this is done via the host field and in OpenAPI V3 it is part of the servers field.

By default, the value for these fields is determined by X-Forwarded headers, X-Forwarded-Host & X-Forwarded-Proto.

This can be overridden by disabling the lookup of X-Forwarded headers and by specifying your own host, port & scheme on the REST configuration.

    .apiProperty("schemes", "https");


In the Apache Camel distribution we ship the camel-example-openapi-cdi and camel-example-spring-boot-rest-openapi-simple which demonstrates using this OpenApi component.

Spring Boot Auto-Configuration

When using openapi-java with Spring Boot make sure to use the following Maven dependency to have support for auto configuration:

  <!-- use the same version as your Camel core version -->

The component supports 1 options, which are listed below.

Name Description Default Type


Enables Camel Rest DSL to automatic register its OpenAPI (eg swagger doc) in Spring Boot which allows tooling such as SpringDoc to integrate with Camel.