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

问题描述

我正在使用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上查看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-path=/**

URI

http://localhost:8003/api-doc

我使用的是骆驼版本:3.9.0


这个解决方案很好用!!!!