【问题标题】:Call REST URL Using Camel使用 Camel 调用 REST URL
【发布时间】:2015-07-31 08:21:24
【问题描述】:

我需要打电话给我当地的骆驼休息服务。

当我从浏览器调用 URL 时,我得到了响应。

举例

http://localhost:8081/buzzor/secure/buzzorapp/getAvailableLanguages

我得到了结果:

 [
    {
        "name": "English",
        "value": "en"
    },
    {
        "name": "मराठी",
        "value": "mr"
    },
    {
        "name": "ગુજરાતી",
        "value": "gu"
    },
    {
        "name": "தமிழ்",
        "value": "ta"
    },
    {
        "name": "हिन्दी",
        "value": "hi"
    },
    {
        "name": "Français",
        "value": "fr"
    },
    {
        "name": "తెలుగు",
        "value": "te"
    }
]

现在我需要从 Camel 调用相同的 REST URL,为此我创建了一个路由。

<camelContext xmlns="http://camel.apache.org/schema/spring" trace="false">
    <route>
        <from uri="direct:start" />
        <to uri="http://localhost:8081/buzzor/secure/buzzorapp/getAvailableLanguages" />
    </route>
</camelContext>

如果我运行项目 URL 后没有被调用。请告诉我我在哪个地方犯了错误。在控制台站点上我只得到输出:

[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] <<< camel-maven-plugin:2.15.1:run (default-cli) < test-compile @ CXF-Sample <<<
[INFO] 
[INFO] --- camel-maven-plugin:2.15.1:run (default-cli) @ CXF-Sample ---
[INFO] Using org.apache.camel.spring.Main to initiate a CamelContext
[INFO] Starting Camel ...
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

【问题讨论】:

  • 从错误看来,这不是骆驼的问题,而是您已包含在构建路径中的 slf4j jar 文件。请验证版本或使用slf4j-simple-1.6.1.jar或1.6+
  • 同意,但为什么没有调用服务/URL?
  • 你不是在向 REST 服务发送而不是从 REST 服务调用吗?如果你在路由声明中更改 tofrom 会发生什么?
  • 尝试在骆驼路由中添加骆驼日志组件,看看是否收到来自 Rest Service 调用的响应,如果没有收到响应,则需要设置 Header跨度>

标签: java xml rest apache-camel


【解决方案1】:
public static void main(String[] args) {
    CamelContext context = new DefaultCamelContext();

    try {
        ProducerTemplate template = context.createProducerTemplate();
        context.start();

        Exchange exchange = template
                .request(
                        "http://localhost:8081/buzzor/secure/buzzorapp/getAvailableLanguages",
                        new Processor() {
                            public void process(Exchange exchange)
                                    throws Exception {
                            }
                        });

        if (null != exchange) {
            Message out = exchange.getOut();
            System.out.println(out.getBody().toString());
            int responseCode = out.getHeader(Exchange.HTTP_RESPONSE_CODE,
                    Integer.class);
            System.out.println("Response: " + String.valueOf(responseCode));
        }

        Thread.sleep(1000 * 3);
        context.stop();
    } catch (Exception ex) {
        System.out.println("Exception: " + ex);
    }

    System.out.println("DONE!!");
} 

【讨论】:

    【解决方案2】:

    看起来您正在使用direct 端点作为消费者。这意味着您需要向direct:start 发送一个交换来触发http get。

    如果使用只运行一次的计时器呢?

    <camelContext xmlns="http://camel.apache.org/schema/spring" trace="false">
      <route>
        <from uri="timer:foo?repeatCount=1" />
        <to uri="http://localhost:8081/buzzor/secure/buzzorapp/getAvailableLanguages" />
      </route>
    </camelContext>
    

    此路由将运行并调用一次 http 端点。

    【讨论】:

    • 你救了我。我一直在寻找一种配置来每天从 rest API 读取 JSON。我没有找到它的方法。
    猜你喜欢
    • 2018-07-02
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-01
    • 1970-01-01
    • 2020-10-02
    相关资源
    最近更新 更多