java - 在 spring boot 中使用现有的 http 服务器作为 Camel 端点

标签 java tomcat spring-boot apache-camel httpserver

我有一个使用 spring boot starter web 的 spring boot 应用程序。这将创建一个正在运行的 Tomcat 实例并设置在端口上运行的 http 服务器。在我的 Camel route ,我想使用这个 http 服务器作为 http 请求的组件,但我不知道如何使用它。我看到很多配置 jetty 实例并从中使用的示例,但实际上我不会运行两个 http 服务器吗?我只想拥有一个。我假设 http 服务器已经自动连接,因为我可以使用其他 spring 代码(例如 RestController)使用它,并且我也可以在我的 spring 引导日志中看到它启动。

@Component
public class ExampleRoute extends RouteBuilder
{
    @Override
    public void configure() throws Exception
    {

        //@formatter:off

        from( <want to take in an http request here> )
            .log( LoggingLevel.INFO, log, "Hello World!" );

        //@formatter:on

    }
}

最佳答案

这里有一个例子:https://github.com/camelinaction/camelinaction2/tree/master/chapter7/springboot-camel

您可以注册一个 ServletRegistrationBean 来使用 Spring Boot 设置 Camel Servlet。

@Bean
ServletRegistrationBean camelServlet() {
    // use a @Bean to register the Camel servlet which we need to do
    // because we want to use the camel-servlet component for the Camel REST service
    ServletRegistrationBean mapping = new ServletRegistrationBean();
    mapping.setName("CamelServlet");
    mapping.setLoadOnStartup(1);
    // CamelHttpTransportServlet is the name of the Camel servlet to use
    mapping.setServlet(new CamelHttpTransportServlet());
    mapping.addUrlMappings("/camel/*");
    return mapping;
}

然而,对于 Camel 2.19,我们计划让这更简单并且 OOTB:https://issues.apache.org/jira/browse/CAMEL-10416

然后你可以做

from("servlet:foo")
  .to("bean:foo");

调用 Camel 路由的 HTTP url 将是 http:localhost:8080/camel/foo

关于java - 在 spring boot 中使用现有的 http 服务器作为 Camel 端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40492458/

相关文章:

spring - 如何在 Spring Boot 测试中指定 text/plain;charset=UTF-8 的媒体类型

java - 计算单元格大小并绘制(中间有线)

java - 如何在内部测试类中注入(inject)spring上下文?或者还有什么其他解决方法?

java - 如何向 java 中的许多方法添加类似的功能?

java - 如何限制字符串中 "/"的个数

java - 返回 404 的 Rest Web 服务

spring-boot - 如何使 Redis 存储库具有事务性

java - 无法关闭在 Spring Tool Suite 中创建的基于 java 的 Web 服务

tomcat - IIS 重写和重定向

java - ValidateJarFile(D :\path\) - jar not loaded. 请参阅 Servlet 规范 3.0,第 10.7.2 节。违规类 : javax/servlet/Servlet. 类