有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Rest DSL路由无法启动路由,因为同一端点不允许有多个使用者

我正在尝试通过一个示例来支持spring boot Hello World REST DSL camel应用程序。当我尝试通过spring boot启动应用程序时,我收到以下异常:

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.4.RELEASE:run (default-cli) on project helloworld-example: An exception occurred while running. null: InvocationTargetException: org.apache.camel.FailedToStartRouteException: Failed to start route route3 because of Multiple consumers for the same endpoint is not allowed: direct://hello -> [Help 1]

从我所看到和理解的情况来看,我只配置了一个路由,它将我的“/hello”路由链接到执行方法的bean

我的路线课:

package my.project.route;

import my.project.model.ResponseObject;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.rest.RestBindingMode;
import org.springframework.stereotype.Component;

@Component
public class MyRoute extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        // configures REST DSL to use servlet component and in JSON mode
        restConfiguration()
                .component("servlet")
                .bindingMode(RestBindingMode.json);

        // REST DSL with a single GET /hello service
        rest()
                .get("/hello")
                .to("direct:hello");

        // route called from REST service that builds a response message
        from("direct:hello")
                .log("Hello World")
                .bean(this, "createResponse");
    }

    public ResponseObject createResponse() {
        ResponseObject response = new ResponseObject();
        response.setResponse("Hello World");
        response.setName("stack overflow");
        return response;
    }
}

我的申请代码

package my.project;

import org.apache.camel.component.servlet.CamelHttpTransportServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@SpringBootApplication
@Configuration
@ComponentScan("my.project")
public class Application {

    /**
     * A main method to start this application.
     */
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public ServletRegistrationBean camelServletRegistrationBean() {
        ServletRegistrationBean registration = new ServletRegistrationBean(new CamelHttpTransportServlet(),"/camel/*");
        registration.setName("CamelServlet");
        return registration;
    }

}

POM:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>my.project</groupId>
  <artifactId>helloworld-example</artifactId>
  <version>1.0-SNAPSHOT</version>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.0.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <name>Fabric8 :: Quickstarts :: Spring-Boot :: Camel</name>
  <description>Spring Boot example running a Camel route</description>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>



    <!-- Fuse 6.3 GA version for Spring Boot -->
    <!--<spring-boot.version>1.4.1.RELEASE</spring-boot.version>-->
    <!-- Fuse 7 EA / GA version for Spring Boot -->
    <spring-boot.version>1.5.4.RELEASE</spring-boot.version>

    <maven-compiler-plugin.version>3.3</maven-compiler-plugin.version>
    <maven-surefire-plugin.version>2.18.1</maven-surefire-plugin.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <version>1.5.4.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
      <version>1.5.4.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-spring-boot-starter</artifactId>
      <version>2.25.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-servlet-starter</artifactId>
      <version>2.25.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-jackson-starter</artifactId>
      <version>2.25.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-swagger-java-starter</artifactId>
      <version>2.25.1</version>
    </dependency>

  </dependencies>

  <build>
    <defaultGoal>spring-boot:run</defaultGoal>

    <plugins>

      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>${maven-compiler-plugin.version}</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>${maven-surefire-plugin.version}</version>
        <inherited>true</inherited>
        <configuration>
          <excludes>
            <exclude>**/*KT.java</exclude>
          </excludes>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>${spring-boot.version}</version>
        <executions>
          <execution>
            <goals>
              <goal>repackage</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

    </plugins>

  </build>

  <!-- used for adding maven repositories to download Fuse JARs -->
  <profiles>
    <profile>
      <id>fuse.repos</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>

      <repositories>
        <repository>
          <id>maven.central</id>
          <name>Maven Central</name>
          <url>https://repo1.maven.org/maven2</url>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
          <releases>
            <enabled>true</enabled>
            <updatePolicy>never</updatePolicy>
          </releases>
        </repository>


      </repositories>

      <pluginRepositories>
        <pluginRepository>
          <id>maven.central</id>
          <name>Maven Central</name>
          <url>https://repo1.maven.org/maven2</url>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
          <releases>
            <enabled>true</enabled>
            <updatePolicy>never</updatePolicy>
          </releases>
        </pluginRepository>

      </pluginRepositories>
    </profile>
  </profiles>

</project>

共 (1) 个答案

  1. # 1 楼答案

    首先,在应用程序类中不需要camelServletRegistrationBean()bean。这仅适用于camel 2.19及以下版本。而是在应用程序中添加camel.component.servlet.mapping.context-path=/*。属性文件

    此外,还可以去掉@ComponentScan。因为在路由上有@Component注释,spring会自动将其添加到上下文中