当前位置: 首页 > 知识库问答 >
问题:

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

皮骏
2023-03-14

我试图通过一个示例来启动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]
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个答案

慕河
2023-03-14

首先,Application类中不需要CamelServletRegistrationBean()bean。这仅适用于骆驼2.19及以下版本。相反,在application.properties文件中添加camel.component.servlet.mapping.context-path=/*

此外,您还可以删除@ComponentScan。由于您在路由上有一个@component注释,spring会自动将其添加到上下文中。

 类似资料:
  • 我在 Openshift Online v3 入门计划上部署了一个应用程序,该应用程序(过去)运行良好,直到昨天。昨天我不得不发布我的应用程序的新版本。显然,该平台在重新部署它时遇到了一些问题,我不得不取消一些似乎锁定或不断重启的进程。最后,我设法让我的 pod 使用新版本运行,日志看起来很好。 现在的问题是我的应用程序不再暴露。当点击分配给我的URL时,我得到了臭名昭著的“不可用”OO页面: 我

  • react&React-Router的新功能。我正在使用react-router-4 我有以下组件-login-home-header-sidebar-content 登录组件没有标题或侧栏。 这就是我的路由 应用程序JS 然后在我的Home组件中,我有侧边栏和内容。 home.js呈现方法 侧边栏组件有一个链接,该链接具有“to”值“/home/dashboard”。 不幸的是,这并不奏效。单击

  • 我在web.php添加了以下路线,但它不起作用。 我的控制器如下所示,我正在使用Ajax发送数据,但收到的错误是Method not allowed exception。 Ajax代码----------------

  • 为了定义辅助路由,我们必须首先添加一个命名的路由出口,其中要呈现辅助路由的内容。 接下来,我们必须定义到应用程序的辅助路由的链接,以导航和呈现内容。 每个辅助路由是独立的路由,可以拥有: 自己的辅助路由 自己的浏览器历史记录栈

  • 将路由链接到参数 显示特定产品详细信息的组件的路由需要该产品ID的路由参数。我们可以使用以下实现: 注意:product-details路由的路径中的 ,它将参数放在路径中。例如,要查看ID为5的产品的产品详细信息页面,必须使用以下URL:localhost:3000/product-details/5 注意,指令传递一个数组,该数组指定路径和路由参数。或者,我们可以使用JS跳转: Product