使用Apache Camel 3(servlet)+ SpringBoot 2的Swagger用户界面

vltsax25  于 2022-11-07  发布在  Apache
关注(0)|答案(1)|浏览(317)

使用Camel 3.8.0和SpringBoot 2.4.2创建了一些REST API(在SpringBoot的默认TomCat上使用Camel的Servlet)。
下面是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>

配置和REST端点-

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()");

    }
}

我试图记录它,并给予一个选项,以测试它使用Swagger用户界面。
如果我运行应用程序并点击http://127.0.0.1:8080/api-docs,我会得到Swagger的API文档。

但是,我不能尝试使用Swagger UI,我如何才能使Swagger UI工作?

我不知道-Swagger-UI是否在此应用程序上工作?如果工作,Swagger-UI的URL是什么?
完整的代码在这里https://github.com/crsardar/hands-on-java/tree/master/hands-on-camel-springboot

vof42yt1

vof42yt11#

要为默认Camel配置、SpringBoot 2.2.7、Camel 3.11.2和Swagger 2.9.2启用Swagger-UI,请执行以下操作:
添加到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>

添加配置文件以启用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();
    }

}

应用程序.属性中添加:

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

路由器配置:

@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");

}

将在地址http://localhost:8080/swagger-ui.html上激活Swagger-UI

相关问题