如何使用 Spring Boot Camel 设置 Swagger 以进行 REST 消息传递

How to setup Swagger with Spring Boot Camel for REST messaging

我正在关注 Swagger Java example,但无法使用 Spring Boot Camel。

我是 运行 Spring Boot Camel 3.4.0,下一个依赖项在 pom.xml:

<!-- Spring Boot -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <exclusions>
        <exclusion>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-undertow</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

    <!-- Camel -->
    <dependency>
      <groupId>org.apache.camel.springboot</groupId>
      <artifactId>camel-spring-boot-starter</artifactId>
    </dependency>
    <dependency>
      <groupId>org.apache.camel.springboot</groupId>
      <artifactId>camel-stream-starter</artifactId>
    </dependency>
    
    <!-- REST -->
      <dependency>
        <groupId>org.apache.camel.springboot</groupId>
        <artifactId>camel-rest-starter</artifactId>
      </dependency>
       <dependency>
         <groupId>org.apache.camel.springboot</groupId>
         <artifactId>camel-servlet-starter</artifactId>
      </dependency>
      <dependency>
         <groupId>org.apache.camel.springboot</groupId>
         <artifactId>camel-jackson-starter</artifactId>
      </dependency>
      <dependency>
         <groupId>org.apache.camel.springboot</groupId>
         <artifactId>camel-jaxb-starter</artifactId>
      </dependency>
      <dependency>
         <groupId>org.apache.camel.springboot</groupId>
         <artifactId>camel-netty-http-starter</artifactId>
      </dependency>
      <dependency>
         <groupId>org.apache.camel.springboot</groupId>
         <artifactId>camel-http-starter</artifactId>
      </dependency>
      <dependency>
         <groupId>org.apache.camel.springboot</groupId>
         <artifactId>camel-jetty-starter</artifactId>
      </dependency>
      <dependency>
         <groupId>org.apache.camel.springboot</groupId>
         <artifactId>camel-undertow-starter</artifactId>
      </dependency>
      <!--<dependency>
         <groupId>org.apache.camel.springboot</groupId>
         <artifactId>camel-rest-swagger-starter</artifactId>
      </dependency>-->
      <dependency>
         <groupId>org.apache.camel</groupId>
         <artifactId>camel-swagger-java</artifactId>
         <version>3.4.0</version>
      </dependency>

我的 Router.java 是下一个:

String listenAddress = "192.168.0.100";
        int listenPort = 8080;

        restConfiguration()
                .component("netty-http")
                .scheme("http")
                .host(listenAddress)
                .bindingMode(RestBindingMode.auto)
                .dataFormatProperty("prettyPrint", "true")
                .port(listenPort)
                .contextPath("/")
                // add swagger api-doc out of the box
                .apiContextPath("/api-doc")
                .apiProperty("api.title", "User API").apiProperty("api.version", "1.2.3")
                // and enable CORS
                .apiProperty("cors", "true");

        // this user REST service is json only
        rest("/user").description("User rest service")
            .consumes("application/json").produces("application/json")
            .get("/{id}").description("Find user by id").outType(User.class)
                .param().name("id").type(path).description("The id of the user to get").dataType("int").endParam()
                .log("Swagger REST header id: ${header.id}");

如果尝试获取 http://192.168.0.100:8080/api-doc 我得到 404。

当使用 http://192.168.0.100:8080/user/123 的 REST GET 时,上面的这条路线应该在 Camel 终端中打印日志,或者我错了吗?看不出少了什么。

上下文路径默认设置为 /camel/,这意味着如果您的其余配置正确,您应该能够在 http://192.168.0.100:8080/camel/api-doc[=14 处看到您的 api-docs =]

要覆盖它,您需要在 application.properties 文件中设置以下 属性。

camel.component.servlet.mapping.context-path= /*

为我添加配置:

在路线中

String listenAddress = "localhost"; int listenPort = 8003;

    restConfiguration()
            .component("servlet")
            .scheme("http")
            .host(listenAddress)
            .bindingMode(RestBindingMode.auto)
            .dataFormatProperty("prettyPrint", "true")
            .port(listenPort)
            .contextPath("/")
            // add swagger api-doc out of the box
            .apiContextPath("/api-doc")
            .apiProperty("api.title", "User API").apiProperty("api.version", "1.2.3")
            // and enable CORS
            .apiProperty("cors", "true");

在application.properties

server.port=8003

camel.component.servlet.mapping.context-路径=/**

URI

http://localhost:8003/api-doc

我使用的是骆驼版本:3.9.0


这个解决方案工作正常!!!!