如何在Camel HTTP Proxy中获取和设置参数

时间:2014-04-26 06:53:25

标签: java http proxy apache-camel

让我们举个例子,我有一个代码:

       from(servlet://abc?id={id}&name={name}).process(new Processor(){
     @Override
        public void process(Exchange arg0) throws Exception {
            id = arg0.getIn().getHeader("id", String.class);
            id_type = arg0.getIn().getHeader("name",String.class);

            System.out.println(id);
            System.out.println(name);
            String url = "//example.com/"+id+"?name="+name;
            System.out.println(url);

            /*Thread.sleep(10000);*/

        }.setHeader(Exchange.HTTP).to("http:"+url+"&bridgeEndpoint=true&throwExceptionOnFailure=false)"

我在那里看不到我的网址。它显示空值。如何解决这个问题呢?我曾经在Exchange标头中设置此字符串,但它给了我java.lang.IllegalArgumentException:

2 个答案:

答案 0 :(得分:4)

尝试以下路线:

from("servlet://abc")  
    .process(new Processor(){
        @Override
        public void process(Exchange exchange) throws Exception {
            // Camel will populate all request.parameter and request.headers, 
            // no need for placeholders in the "from" endpoint
            String id = exchange.getIn().getHeader("id", String.class);
            String name = exchange.getIn().getHeader("name", String.class);           

            // This URI will override http://dummyhost
            exchange.getIn().setHeader(Exchange.HTTP_URI, "http://example.com");

            // Add input path. This will override the original input path.
            // If you need to keep the original input path, then add the id to the 
            // URI above instead
            exchange.getIn().setHeader(Exchange.HTTP_PATH, id);

            // Add query parameter such as "?name=xxx"
            exchange.getIn().setHeader(Exchange.HTTP_QUERY, "name="+name);     
    }
    .to("http://dummyhost")

如果您的请求是http://localhost:8080/hello/world?id=111&name=moon,则应将其转发至http://example.com/111?name=moon

答案 1 :(得分:0)

当驼峰设置路线时,无法知道网址。

您可以使用Exchange.HTTP_URI邮件头覆盖http端点的设置。