簡體   English   中英

使用駱駝調用REST URL

[英]Call REST URL Using Camel

我需要通過駱駝給我的當地休息處打電話。

當我從瀏覽器中調用URL時,我得到了答復。

例如

HTTP://本地主機: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"
    }
]

現在我需要從駱駝調用相同的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!!");
} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM