在apache camel配置中动态添加<setbody>值

时间:2016-09-02 08:24:01

标签: java rest apache-camel

我正在尝试使用camel向webservice发送请求,因为我已经声明了camel-config.xml,如下所示

<routes xmlns="http://camel.apache.org/schema/spring">
    <route>
        <!-- incoming requests from the servlet is routed -->
        <from uri="servlet:hello" />
        <choice>
            <when>
                <!-- is there a header with the key name? -->
                <setHeader headerName="CamelHttpMethod">
                    <constant>POST</constant>
                </setHeader>
                <header>advertisement</header>
                <setBody> <simple>{"id":"k"}</simple> </setBody>
                <!-- yes so return back a message to the user -->
                <to
                    uri="cxfrs:http://xxxxxxxxx:8080/xxx/rest/xxx/xxxxxx" />
            </when>
            <otherwise>
                <!-- if no name parameter then output a syntax to the user -->
                <transform>
                    <constant>Add a name parameter to uri, eg
                        ?name=image_xx.xx
                    </constant>
                </transform>
            </otherwise>
        </choice>
    </route></routes>

在这里我将值设置为静态可以有人说如何动态地将值设置为<setBody></setBody>我将通过HTML表单作为POST请求发送

2 个答案:

答案 0 :(得分:0)

你可以使用javascript + java将任何内容写入正文(参见我的例子)

...
...

    <setBody>
        <javaScript>
            var m = new java.util.HashMap();
            m.put("aField", <<<Here java code!>>>>);
            m.put("anotherField", <<<Here another java code!>>>>);
    
            m;
        </javaScript>
    </setBody>
    <marshall ref="json" />
    <to uri="....... "/>
...
...

地图将解析为json字符串! 国王问候

FabryProg

答案 1 :(得分:0)

我搜索并发现

 <setBody>
        <simple>${in.headers.advertisement}</simple>
    </setBody>

这很好用