【问题标题】:Cannot change REST context path in Apache Camel 3.14.0 Spring Boot app无法在 Apache Camel 3.14.0 Spring Boot 应用程序中更改 REST 上下文路径
【发布时间】:2022-10-19 22:54:25
【问题描述】:

依赖项:

dependencies {

    implementation("org.apache.camel:camel-core:3.14.0")
    implementation("org.apache.camel:camel-openapi-java:3.14.0")

    implementation("com.fasterxml.jackson.core:jackson-core:2.13.1")
    implementation("com.fasterxml.jackson.core:jackson-databind:2.13.1")
    implementation("com.fasterxml.jackson.core:jackson-annotations:2.13.1")

    implementation("org.springframework.boot:spring-boot-starter-web")

    implementation("org.apache.camel.springboot:camel-spring-boot-starter:3.14.0")
    implementation("org.apache.camel.springboot:camel-servlet-starter:3.14.0")
    implementation("org.apache.camel.springboot:camel-jackson-starter:3.14.0")
    implementation("org.apache.camel.springboot:camel-http-starter:3.14.0")
    implementation("org.apache.camel.springboot:camel-bean-validator-starter:3.14.0")
}

尝试从 prop 文件更改: apache-camel-spring-boot/src/main/resources/application.properties:

camel.component.servlet.mapping.contextPath=/test/*
camel.component.servlet.mapping.context-path=/test/*

apache-camel-spring-boot/src/main/resources/application.yml:

camel:
  component:
    servlet:
      mapping:
        contextPath: /test/*

实际的 REST 配置:

@Component
public class CamelRouter extends RouteBuilder {

  @Override
  public void configure() throws Exception {

    restConfiguration()
        .component("servlet")
        .contextPath("test")
        .host("localhost")
        .port(8080)
        .bindingMode(RestBindingMode.json)
        .enableCORS(true);

    rest("/users").description("User REST service")
        .produces("application/json")
        .get()
        .outType(String.class)
        .to("bean:myService?method=find")
  }
}

但唯一有效的 URL 是:http://localhost:8080/camel/users

当我更改了 Camel 的上下文路径时,为什么 http://localhost:8080/test/users 返回 404?

【问题讨论】:

    标签: java spring spring-boot apache-camel


    【解决方案1】:

    添加到apache-camel-spring-boot/src/main/resources/application.properties

    camel.servlet.mapping.context-path=/*
    

    【讨论】:

      【解决方案2】:

      对已接受答案的小补充。在 Spring Boot 2.7.2 或 Apache Camel 3.18.0 以下,关键是:

      camel.component.servlet.mapping.contextPath=/api/*

      否则就是

      camel.servlet.mapping.context-path=/api/*

      正如托马斯Apache Camel Rest endpoint with DSL in Spring Boot adds /camel to the path所说

      所以在我的情况下,我使用的是 Spring Boot 版本 2.4.0 和 Apache Camel 3.7.3,我不得不使用“camel.component.servlet.mapping.context-path”。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-08-17
        • 1970-01-01
        • 2018-05-06
        • 2013-12-22
        • 1970-01-01
        • 2020-08-30
        • 1970-01-01
        相关资源
        最近更新 更多