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:

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.

Using with Camel 2.15.x

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

The name of the servlet is org.apache.camel.component.swagger.DefaultCamelSwaggerServlet.

Using with Camel 2.14.x

The Swagger servlet is integrated with Spring or ServletListener Component

ComponentServlet ClassName
springorg.apache.camel.component.swagger.spring.SpringRestSwaggerApiDeclarationServlet 
servletlistenerorg.apache.camel.component.swagger.servletlistener.ServletListenerRestSwaggerApiDeclarationServlet

The servlet support the same options when using spring or servletlistener.

The servlets above from Camel 2.14.x is deprecated and replaced with a single default servlet from Camel 2.15 onwards.

 

For example when using Spring you need to configure the org.apache.camel.component.swagger.spring.SpringRestSwaggerApiDeclarationServlet in the WEB-INF/web.xml file as shown below:

If you use Camel 2.15 onwards then just use the default servlet in any kind of environment.

Options

The org.apache.camel.component.swagger.RestSwaggerApiDeclarationServlet and org.apache.camel.component.swagger.DefaultCamelSwaggerServlet supports the following options which can be configured as context-param in the web.xml file.

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

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

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.

CorsFilter

If you use the swagger ui to view the REST api then you likely need to enable support for CORS. This is needed if the swagger ui is hosted and running on another hostname/port than the actual REST apis. When doing this the swagger ui needs to be allowed to access the REST resources across the origin (CORS). The CorsFilter adds the necessary HTTP headers to enable CORS.

To use CORS adds the following filter org.apache.camel.component.swagger.RestSwaggerCorsFilter to your web.xml.

The CorsFilter sets the following headers for all requests

  • Access-Control-Allow-Origin = *
  • Access-Control-Allow-Methods = GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, CONNECT, PATCH
  • Access-Control-Max-Age = 3600
  • Access-Control-Allow-Headers = Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers

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

 


© 2004-2015 The Apache Software Foundation.
Apache Camel, Camel, Apache, the Apache feather logo, and the Apache Camel project logo are trademarks of The Apache Software Foundation. All other marks mentioned may be trademarks or registered trademarks of their respective owners.
Graphic Design By Hiram