【问题标题】:Swagger UI with Apache Camel 3 (servlet) + SpringBoot 2Swagger UI 与 Apache Camel 3 (servlet) + SpringBoot 2
【发布时间】:2021-03-12 23:47:43
【问题描述】:

Camel + SpringBoot + Swagger 的新手。 使用 Camel 3.8.0 和 SpringBoot 2.4.2 创建了一些 REST API(在 SpringBoot 的 Default 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 UI 对其进行测试的选项。

如果我运行应用程序并点击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

【问题讨论】:

  • 在互联网上搜索了很多,但可以找到很多关于“Swagger UI 与 Camel 3 (servlet) + SpringBoot 2”组合的信息,所有工作示例都使用旧版本的 Camel 和 SpringBoot

标签: java spring-boot apache-camel swagger swagger-ui


【解决方案1】:

为默认的 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();
    }

}

添加application.properties

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

}

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

【讨论】:

  • 从哪里获得 swagger-ui.html 文件?只有有了这个依赖项,我才能让它工作。谢谢
猜你喜欢
  • 2021-11-12
  • 1970-01-01
  • 1970-01-01
  • 2019-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-07
  • 1970-01-01
相关资源
最近更新 更多