使用Camel调用REST URL

时间:2015-05-19 13:15:55

标签: java xml rest apache-camel

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

当我从浏览器调用URL时,我收到了回复。

例如

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

我得到了一个结果:

<membership defaultProvider="UmbracoMembershipProvider" userIsOnlineTimeWindow="15">
  <providers>
    <clear />
    <add name="UmbracoMembershipProvider" type="Umbraco724.Providers.MyMembersMembershipProvider, Umbraco724"  minRequiredNonalphanumericCharacters="0" minRequiredPasswordLength="4" useLegacyEncoding="true" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" defaultMemberTypeAlias="Visitor" passwordFormat="Hashed" />
    <add name="UsersMembershipProvider" type="Umbraco.Web.Security.Providers.UsersMembershipProvider, Umbraco" minRequiredNonalphanumericCharacters="0" minRequiredPasswordLength="4" useLegacyEncoding="true" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" passwordFormat="Hashed" />
  </providers>
</membership>

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

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

执行此操作后如果我运行项目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>

2 个答案:

答案 0 :(得分:5)

您似乎使用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端点一次。

答案 1 :(得分:0)

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