属性 在 application.yml 的属性中找不到键 [http-client.timers['http-get'].name]

Property with key [http-client.timers['http-get'].name] not found in properties from application.yml

我有一个 Spring 引导应用程序,它使用 Apache Camel 来定义和控制数据路由。这些路由是通过 XML DSL 定义的,并具有 属性 占位符以允许定义路由的可变性。

我在尝试定义和使用项目集合时遇到错误:

Caused by: java.lang.IllegalArgumentException: Property with key [http-client.timers['http-get'].name] not found in properties from text: timer:{{http-client.timers['http-get'].name}}?delay={{http-client.timers['http-get'].start-delay}}&fixedRate=true&period={{http-client.timers['http-get'].period}}&repeatCount={{http-client.timers['http-get'].repeat-count}}

application.yml:

---

camel:
  springboot:
    name: MissionServices
    main-run-controller: true

http-client:
  server:
    host: localhost
    port: 9100
  endpoint: chars?size=500
  timers:
    - http-get:
      name: http-get
      start-delay: 0
      period: 1000
      repeat-count: 5
    - http-post:
      name: http-post
      start-delay: 0
      period: 5000
      repeat-count: 5

驼色-context.xml:

...
  <camelContext id="camel-context"
    xmlns="http://camel.apache.org/schema/spring">

    <route id="http-get">
      <from
        uri="timer:{{http-client.timers['http-get'].name}}?delay={{http-client.timers['http-get'].start-delay}}&amp;fixedRate=true&amp;period={{http-client.timers['http-get'].period}}&amp;repeatCount={{http-client.timers['http-get'].repeat-count}}" />
      <log loggingLevel="INFO" message="start - http-get" />
      <setHeader name="HTTP_METHOD">
        <constant>GET</constant>
      </setHeader>
      <to
        uri="http:{{http-client.server.host}}:{{http-client.server.port}}/{{http-client.endpoint}}" />
      <log loggingLevel="INFO" message="end - http-get" />
    </route>

    <route id="http-post">
      <from uri="direct:start-http-post" />
      <log loggingLevel="INFO" message="start - http-post" />
      <setHeader name="HTTP_METHOD">
        <constant>POST</constant>
      </setHeader>
      <setHeader name="CONTENT_TYPE">
        <constant>application/json</constant>
      </setHeader>
      <to
        uri="http:{{http-client.server.host}}:{{http-client.server.port}}/{{http-client.endpoint}}" />
      <log loggingLevel="INFO" message="end - http-post" />
    </route>

  </camelContext>
...

是我的 YAML 结构不正确,还是我在路由定义中使用了错误的语法来访问属性?

好吧,http-client.timers['http-get'].name 声明 name 嵌套在 http-get 中,而在您的 YAML 中:

  timers:
    - http-get:
      name: http-get
      start-delay: 0
      period: 1000
      repeat-count: 5

name:http-get: 的兄弟(它们共享相同的缩进级别, 了解有关这种情况下缩进处理的详细信息)。此外,您在此处开始一个序列(使用 -),但该路径不使用序列中的任何索引。你可能想要

  timers:
    http-get:
      name: http-get
      start-delay: 0
      period: 1000
      repeat-count: 5

首先,yml 中的缩进不正确。

其次,结构不太对。您正在寻找的是带有字段名称、开始延迟、周期和重复计数的对象映射。但是您已将其声明为列表。

您正在尝试通过键 (http-get) 查找对象。您不能使用键搜索列表。您可以使用索引搜索列表。你需要的是一张地图。

正确的 yaml 结构应该是

http-client:
  server:
    host: localhost
    port: 9100
  endpoint: chars?size=500
  timers:
     http-get:
        name: http-get
        start-delay: 0
        period: 1000
        repeat-count: 5
     http-post:
        name: http-post
        start-delay: 0
        period: 5000
        repeat-count: 5

要访问您正在寻找的值应该是这样的

{{http-client.timers.http-get.name}}