Apache的骆驼简单的HTTP的例子 [英] Apache camel simple http example

查看:194
本文介绍了Apache的骆驼简单的HTTP的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是pretty新的骆驼。我一直在试图获取从http源数据。
这里是我的code:

I am pretty new with Camel. I have been trying to fetch a data from http source. Here's my code:

from("timer://runOnce?repeatCount=1")
    .to("http4://webservice.com/example.xml")
    .process(new structureXML())
    .to("mock:resource")
    .stop();

class structureXML implements Processor {

    public void process(Exchange httpExchange) throws Exception {
        String httpres = httpExchange.getIn().getBody(String.class);
        String[] lines = httpres.split("\n");
        Pattern p = Pattern.compile("<Map Key='(.+)' Value='(.+)'/>");
        HashMap<String, Integer> mapdata = new HashMap<String, Integer>();

        for(String line : lines) {
            Matcher m = p.matcher(line);

            if(m.find())
                mapdata.put(m.group(1), Integer.parseInt(m.group(2)));
        }

        httpExchange.getIn().setBody(mapdata);
    }
}

好例子作品的权利,但我想知道的可能途径,以进一步改善这种情况(如XML处理使用XPath等),我也想知道我可以在java对象存储邮件里面的方法这样我就可以在另一条路线使用(如:直接:资源,而不是模拟)

Well the example works right but I want to know about the possible ways to further improve this situation(e.g xml processing using xpath and etc), also I want to know about the ways which I can store the java object inside the message so I can use it in another route(e.g: direct:resource instead of mock)

推荐答案

关于Java对象:

更多信息可以在这里找到: http://camel.apache.org/data-format html的

More information can be found here: http://camel.apache.org/data-format.html


  • JAXB

  • 的XStream

  • BeanIO

  • 的JiBX

  • XmlBeans的

这些数据格式将是XML转换成POJO非常有用的。
我建议你​​尝试的 BeanIO (详细文档,例子很多,等等)。

These data formats will be very useful for transforming XML to POJO. I recomend you to try BeanIO (detailed documentation, many examples, etc).

有关XPath的:

这很难说没有网络的服务响应指定的任何东西。

it's hard to tell anything specified without web-service response.

例如:

setBody().xpath("/soap:Envelope/soap:Body/s:insertResponse/s:data",
            XmlNamespaces.getNamespace()).

关于你的例子:

您通常需要设置很多的属性和头(HTTP请求之前),所以它工作得很好。例如:

You usually need to set a lot of properties and header (before http request), so it worked fine. Example:

    setProperty(Exchange.CONTENT_TYPE).constant("application/soap+xml").
    setProperty(Exchange.CONTENT_ENCODING).constant("gzip").
    setProperty(Exchange.CHARSET_NAME).constant("utf-8").
    setHeader(Exchange.CONTENT_TYPE).exchangeProperty(Exchange.CONTENT_TYPE).

和我没有看到创建请求的Web服务。这是很容易使用的速度的帮助下做 HTTP://camel.apache .ORG / velocity.html ),或者,也许,使用SOAP日期格式(的http:// camel.apache.org/soap.html )。

And I don't see creating the request to web-service. It is easy to do with the help of velocity (http://camel.apache.org/velocity.html), or, maybe, using SOAP date format (http://camel.apache.org/soap.html).

您可以使用码头 http://camel.apache.org/ jetty.html ),而不是 http4 (对我来说更容易)

You can use jetty (http://camel.apache.org/jetty.html) instead of http4 (for me it's easier)

这篇关于Apache的骆驼简单的HTTP的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆