使用Camel调用REST URL



我需要从camel调用本地休息服务。

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

例如

http://localhost: 8081/buzzor/安全/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.

看起来您正在使用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端点一次。

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!!");
} 

最新更新