首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在application.yml的属性中找不到具有键[http-client.timers['http-get'].name]的属性

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

Stack Overflow用户
提问于 2020-04-08 20:53:15
回答 2查看 208关注 0票数 0

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

当我试图定义和使用一个项目集合时,我得到了一个错误:

代码语言:javascript
复制
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:

代码语言:javascript
复制
---

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

camel-context.xml:

代码语言:javascript
复制
...
  <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的结构不正确,还是我在路由定义中使用了错误的语法来访问属性?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-04-08 21:09:52

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

其次,结构不太正确。您正在寻找的是一个对象映射,其中包含字段name、start-delay、period和repeat-count。但您已将其声明为列表。

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

正确的yaml结构应为

代码语言:javascript
复制
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

要访问您正在查找的值,应该如下所示

代码语言:javascript
复制
{{http-client.timers.http-get.name}}
票数 1
EN

Stack Overflow用户

发布于 2020-04-08 21:01:45

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

代码语言:javascript
复制
  timers:
    - http-get:
      name: http-get
      start-delay: 0
      period: 1000
      repeat-count: 5

name:http-get:的兄弟(它们共享相同的缩进级别,有关这种情况下的缩进处理的详细信息,请使用see this answer )。此外,您在这里启动了一个序列(使用-),但是路径没有对序列进行任何索引。你可能想要

代码语言:javascript
复制
  timers:
    http-get:
      name: http-get
      start-delay: 0
      period: 1000
      repeat-count: 5
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61101151

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档