Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swagger UI with Apache Camel 3 (servlet) + SpringBoot 2

New to Camel + SpringBoot + Swagger. Created some REST APIs using Camel 3.8.0 and SpringBoot 2.4.2 (using Camel's Servlet on Default TomCat of SpringBoot).

Here is pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.2</version>
    <relativePath/>
</parent>

<groupId>com.crsardar.java.apache.camel</groupId>
<artifactId>hands-on-camel-springboot</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>

    <camelVersion>3.8.0</camelVersion>
</properties>

<dependencies>
    <!-- SpringBoot -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- Camel -->
    <dependency>
        <groupId>org.apache.camel.springboot</groupId>
        <artifactId>camel-spring-boot-bom</artifactId>
        <version>${camelVersion}</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-jackson</artifactId>
        <version>${camelVersion}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-servlet-starter</artifactId>
        <version>${camelVersion}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.camel.springboot</groupId>
        <artifactId>camel-spring-boot-starter</artifactId>
        <version>${camelVersion}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.camel.springboot</groupId>
        <artifactId>camel-rest-swagger-starter</artifactId>
        <version>${camelVersion}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-swagger-java</artifactId>
        <version>${camelVersion}</version>
        <exclusions>
            <exclusion>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>*</artifactId>
            </exclusion>
        </exclusions>
    </dependency>


    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>swagger-ui</artifactId>
        <version>2.2.10</version>
    </dependency>
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>webjars-locator</artifactId>
        <version>0.40</version>
    </dependency>


    <!-- Others -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.18</version>
        <scope>compile</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

Configuration and REST end points -

package com.crsardar.java.apache.camel;

import com.crsardar.java.dao.Order;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.rest.RestBindingMode;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;

@Component
public class CamelController extends RouteBuilder {

    @Override
    public void configure() throws Exception {

        restConfiguration()
                .component("servlet")
                .port(8080)
                .host("127.0.0.1")
                .apiContextPath("api-docs")
                .apiContextIdPattern("#name#")
                .apiProperty("api.title", "Test REST API")
                .apiProperty("api.version", "v1")
                .apiProperty("cors", "true")
                .bindingMode(RestBindingMode.json);

        rest().post("/order")
                .produces(MediaType.APPLICATION_JSON_VALUE)
                .type(Order.class)
                .outType(Order.class)
                .to("bean:orderService?method=addOrder(${body})");

        rest().get("/order")
                .produces(MediaType.APPLICATION_JSON_VALUE)
                .to("bean:orderService?method=getOrders()");

    }
}

I am trying to document it and give an option to test it using Swagger UI.

If I run the application and hit http://127.0.0.1:8080/api-docs I am getting Swagger's API documentation.

But, I can not try it using Swagger UI, How can I make Swagger UI working on it?

I do not know - Whether Swagger-UI is working on this app or not? If working, what will be the URL for Swagger-UI?

Complete code is here https://github.com/crsardar/hands-on-java/tree/master/hands-on-camel-springboot

like image 507
CR Sardar Avatar asked Nov 01 '25 15:11

CR Sardar


1 Answers

To enable Swagger-UI for default Camel configuration, SpringBoot 2.2.7, Camel 3.11.2 and Swagger 2.9.2:

Add to your pom.xml

    <!-- swagger -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>${swagger.version}</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>${swagger.version}</version>
        <scope>compile</scope>
    </dependency>

Add Configuration file to enable swagger-ui:

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }

}

Add in application.properties:

springfox.documentation.swagger.v2.path=/camel/api-doc

Router configuration:

@Override
public void configure() throws Exception {
    String contextPath = env.getProperty("camel.servlet.mapping.context-path", "/camel");

    //Common Rest config
    restConfiguration()
            .component("servlet")
            .bindingMode(RestBindingMode.json)
            .contextPath(contextPath)
            .enableCORS(true)

    //Enable swagger endpoint.
            .dataFormatProperty("prettyPrint", "true")
            // add swagger api-doc out of the box
            .apiContextPath("/api-doc")
                .apiProperty("api.title", "API Title")
                .apiProperty("api.version", "v1")
                // and enable CORS
                .apiProperty("cors", "true");

}

Swagger-UI will be active on address http://localhost:8080/swagger-ui.html

like image 51
ironside Avatar answered Nov 03 '25 07:11

ironside