【问题标题】:Making a http request using Apache Camel使用 Apache Camel 发出 http 请求
【发布时间】:2019-02-13 02:45:13
【问题描述】:

我在开始使用 Apache Camel 时遇到了问题。我正在尝试创建一个向公共 API 发出 http 请求的路由。我使用的是现成的项目模板,所有 POM 依赖项都应该是正确的。这是我的路线代码:

    import org.apache.camel.builder.RouteBuilder;
    import org.springframework.stereotype.Component;

    @Component
    public class Routes extends RouteBuilder {
        @Override
        public void configure() {


            from("https://rata.digitraffic.fi/api/v1/train-
            locations/latest/")
            .description("Hello world -route")
            .log("Hello world!")
            .to("mock:out");


        }
    }

所以我希望从 API 中获取一些数据,但现在我只是遇到了构建失败。

【问题讨论】:

    标签: apache rest apache-camel


    【解决方案1】:

    我认为您不能使用from() 中的 URL 请求。 您需要创建一个来自另一个事件的路由,例如 Timer 或使用来自 JMS 的消息。

    为了使用 Apache Camel 发出 HTTP 请求,我使用组件 HTTP4 并在 to() 上声明请求。

    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-http4</artifactId>
      <version>${camel.version}</version>
    </dependency>
    

    下面是一个带有 Timer 组件的示例,它每 15 秒启动一次进程并执行一个 HTTP 请求。

    @Component
    public class Routes extends RouteBuilder {
        @Override
        public void configure() {
    
            from("timer:SimpleTimerName?period=15s")
            .description("Hello world -route")
            .log("Hello world!")
            .to("https4://rata.digitraffic.fi/api/v1/train-locations/latest/");
            .log("This is the status code from the response: ${header.CamelHttpResponseCode}")
            .log("This is the return: ${body}")
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-04
      • 2013-10-08
      • 1970-01-01
      • 1970-01-01
      • 2017-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多