【问题标题】:Spring boot 2.x with Camel 2.25 : Spring specific endpoints not workingSpring boot 2.x with Camel 2.25:Spring 特定端点不起作用
【发布时间】:2020-11-15 07:50:21
【问题描述】:

我有一个带有 spring-boot 2.x 和 camel 2.25 的项目。它有不同的骆驼路线以及很少的 REST 消费者路线。到目前为止一切都很好。

现在我添加了一些带有一些端点的普通 spring-boot @RestController 类。但这些都不起作用(抛出 404)。

当我调查时发现,每个请求都来自 CamelServlet,它完全不知道基于 Spring 的普通 @RestController 端点(但只知道 Camel REST 消费者路由端点)。因此,仅在 @RestController 端点引发此错误,而 Camel REST 端点仍在工作。

下面是我的配置,

spring:
 application:
  name: gateway
 main:
  web-application-type: SERVLET 


server:
 servlet:
  context-path: /gateway
 port: 8080

camel:
 springboot:
  name: gateway
 component:
  servlet:
   mapping:
    enabled: true
    context-path: /*
  mail:
   basic-property-binding: true

下面是我的 POM

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-servlet-starter</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-mail-starter</artifactId>
    </dependency>

我做错了什么吗?有什么建议吗?提前致谢。

【问题讨论】:

  • Bhushan 的回答在这种情况下是合适的,但为了结束对话,我想指出另一个选项,将骆驼路线与@Clause 提到的非骆驼应用程序集成易卜生在博客中(我失去了链接)。对于任何非骆驼应用程序,我们可以使用 Camel 的 FluentProducerTemplate,它可以在 Spring Rest 控制器类中的任何位置使用 EndpointInject 注释自动装配,并可用于将骆驼交换发送到任何骆驼端点。

标签: spring-boot apache-camel camel-rest


【解决方案1】:

这是因为你设置了 context-path: /* 模式意味着骆驼要拦截(因为这个路径是用骆驼注册的)它,在spring servlet调度程序处理它之前,所以如果你想要处理 @Restcontroller 那么你需要为骆驼定义一个单独的上下文路径,例如:context-path:camel-api/*模式,现在骆驼会注册camel-api base route,如果pattern与camel-api URL不同,则由spring-boot处理

@Bean
ServletRegistrationBean servletRegistrationBean() {
    ServletRegistrationBean servlet = new ServletRegistrationBean
      (new CamelHttpTransportServlet(), "camel-api/*");
    servlet.setName("CamelServlet");
    return servlet;
}

或使用属性进行配置。

【讨论】:

  • 感谢您的回答,它真的很有帮助。但是我们真的有办法使用配置属性来配置这两个不同的 url(比如 camel-api/* 和其他)吗?我问的原因是,在 Spring-boot 2.x 环境中,我没有明确配置任何 Servlet bean。
  • camel: springboot: name: gateway component: servlet: mapping: enabled: true context-path: camel-api/* 您可以在此处根据您的路径格式模式设置路径,然后此模式将自动注册到引导休息控制器。可以配置多个相对路径来注册骆驼new ServletRegistrationBean(CamelHttpTransportServlet(), "camel-api/*", "anotherpattern/*");我不确定你将如何使用属性来实现它,因为我没有找到属性自动配置cls,你可以找到那个cls并为它计算属性类型。跨度>
  • 我的建议是,如果您已经在使用骆驼 API 组件,请继续使用它,而不是同时集成 @RestController。
猜你喜欢
  • 2020-10-22
  • 2018-07-08
  • 2018-11-19
  • 2021-09-30
  • 2021-10-17
  • 1970-01-01
  • 2018-08-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多