이 콘텐츠는 선택한 언어로 제공되지 않습니다.

4.5. Swagger Integration


Overview

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:
<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>
Copy to Clipboard Toggle word wrap

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 Swagger in the Apache Camel Component Reference Guide.
  • 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="http://www.osgi.org/xmlns/blueprint/v1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
             http://www.osgi.org/xmlns/blueprint/v1.0.0 
             http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
             http://camel.apache.org/schema/blueprint 
             http://camel.apache.org/schema/blueprint/camel-blueprint.xsd">

    <service interface="javax.servlet.http.HttpServlet">
        <service-properties>
            <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"/>
        </service-properties>
        <bean class="org.apache.camel.component.swagger.DefaultCamelSwaggerServlet" />
    </service>

    
    <camelContext id="log-example-context"
        xmlns="http://camel.apache.org/schema/blueprint">

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

    </camelContext>

</blueprint>
Copy to Clipboard Toggle word wrap
The following parameters are used in the above example as described here:
service
The service element exposes the camel swagger servlet (<bean class="org.apache.camel.component.swagger.DefaultCamelSwaggerServlet"/>) and initializes several servlet properties.
alias
The alias property binds the camel swagger servlet to /api-docs/*.
init-prefix
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").
restConfiguration
In the camelContext element, the restConfiguration element specifies the REST implementation to use. In this case, it is Jetty web servlet on port 8080.
rest
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

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다. 최신 업데이트를 확인하세요.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

Theme

© 2025 Red Hat