有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我正在尝试使用camel向webservice发送请求,因为我已经声明了camel配置。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请求发送吗


共 (1) 个答案

  1. # 1 楼答案

    您可以使用javascript+java将任何内容写入body(参见我的示例)

    ...
    ...
    
        <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字符串! 国王问候

    法布里普罗格