Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Swagger Scala Component (deprecated)

Available as of Camel 2.14

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

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

Warning

This component is deprecated. From Camel 2.16 onwards use the new Java based swagger module Swagger Java

The Scala based camel-swagger module is deprecated, and to be removed in a future release.

Code Block
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-swagger</artifactId>
    <version>x.x.x</version>
    <!-- use the same version as your Camel core version -->
</dependency>

Using with Camel 2.15

...

.x

The default servlet supports any environment using JMX to discover the CamelContext(s) to use.

...

Code Block
  <!-- to setup Camel Swagger api servlet when using Spring -->
  <servlet>
 
    <!-- Camel 2.14.x -->
    <servlet-name>ApiDeclarationServlet</servlet-name>
    <servlet-class>org.apache.camel.component.swagger.spring.SpringRestSwaggerApiDeclarationServlet</servlet-class>
 
    <!-- Camel 2.15 onwards -->
    <servlet-name>ApiDeclarationServlet</servlet-name>
    <servlet-class>org.apache.camel.component.swagger.DefaultCamelSwaggerServlet</servlet-class>

    <!-- Camel 2.14.x -->
    <init-param>
      <param-name>base.path</param-name>
      <param-value>http://localhost:8080/rest</param-value>
    </init-param>
    <init-param>
      <param-name>api.path</param-name>
      <param-value>http://localhost:8080/api-docs</param-value>
    </init-param>
 
    <!-- Camel 2.15 onwards -->
    <init-param>
      <!-- we specify the base.path using relative notation, that means the actual path will be calculated at runtime as
           http://server:port/contextpath/rest -->
      <param-name>base.path</param-name>
      <param-value>rest</param-value>
    </init-param>
    <init-param>
      <!-- we specify the api.path using relative notation, that means the actual path will be calculated at runtime as
           http://server:port/contextpath/api-docs -->
      <param-name>api.path</param-name>
      <param-value>api-docs</param-value>
    </init-param>


    <init-param>
      <param-name>api.version</param-name>
      <param-value>1.2.3</param-value>
    </init-param>
    <init-param>
      <param-name>api.title</param-name>
      <param-value>User Services</param-value>
    </init-param>
    <init-param>
      <param-name>api.description</param-name>
      <param-value>Camel Rest Example with Swagger that provides an User REST service</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
  </servlet>

  <!-- swagger api declaration -->
  <servlet-mapping>
    <servlet-name>ApiDeclarationServlet</servlet-name>
    <url-pattern>/api-docs/*</url-pattern>
  </servlet-mapping>

...

Div
classconfluenceTableSmall

Option

Type

Description

corsBooleanWhether to enable CORS. Notice this only enables CORS for the api browser, and not the actual access to the REST services. Is default false.
Instead of using this option is recommended to use the CorsFilte, see further below.
swagger.versionStringSwagger spec version. Is default 1.2.
base.path

 

String

Required: To setup the base path where the REST services is available. Using Camel 2.14.x this path must be the absolute path to the services. From Camel 2.15 onwards you can use a relative path instead (eg do not start with http/https) and camel-swagger will calculate the absolute base path at runtime, which will be

Code Block
protocol://host:port/context-path/base.path

So using relative paths is much easier. See above for an example.

api.versionStringThe version of the api. Is default 0.0.0.
api.pathString

To setup the path where the API is available (eg /api-docs). Using Camel 2.14.x this path must be the absolute path to the services. From Camel 2.15 onwards you can use a relative path instead (eg do not start with http/https) and camel-swagger will calculate the absolute base path at runtime, which will be

Code Block
protocol://host:port/context-path/api.path

So using relative paths is much easier. See above for an example.

api.titleStringRequired. The title of the application.
api.descriptionStringRequired. A short description of the application.
api.termsOfServiceUrlStringA URL to the Terms of Service of the API.
api.contactStringAn email to be used for API-related correspondence.
api.licenseStringThe license name used for the API.
api.licenseUrlStringA URL to the license used for the API.

...

Notice this is a very simple CORS filter. You may need to use a more sophisticated filter to set the header values differently for a given client. Or block certain clients etc.

Multiple CamelContexts

Available as of Camel 2.16

When using camel-swagger from Camel 2.16 onwards then it supports detecting all the running CamelContexts in the same JVM. These contexts are listed in the root path, eg `/api-docs` as a simple list of names in json format. To access the swagger documentation then the context-path must be appended with the Camel context id, such as `api-docs/myCamel`.

Examples

In the Apache Camel distribution we ship the camel-example-servlet-rest-tomcat which demonstrates using this Swagger component.

See Also