【问题标题】:esb mule passing the parameters to the method via httpesb mule 通过 http 将参数传递给方法
【发布时间】:2015-08-18 01:03:21
【问题描述】:

我有一个测试方法:

@Test
    public void testHello_with_muleXmlConfig() throws Exception {

        MuleClient client = new MuleClient("mule-config-test.xml");
        client.getMuleContext().start();

        MuleMessage result = client.send("http://127.0.0.1:8080/hello", "some data", null);
        assertNotNull(result);

        assertNull(result.getExceptionPayload());
        assertFalse(result.getPayload() instanceof NullPayload);

        assertEquals("hello", result.getPayloadAsString());
    }

这里(client.send("http://127.0.0.1:8080/hello", "some data", null)),我传递的是参数/data = 'some data'。

我有一堂课:

public class HelloWorld {
    public String sayHello() {
        return "hello";
    }
}   

在 mule-config.xml 中作为 spring bean 公开:

<spring:bean id="helloWorld" class="org.mule.application.hello.HelloWorld"/>

<flow name="HelloWorld">
        <inbound-endpoint address="http://127.0.0.1:8080/hello"/>
        <invoke method="sayHello" object-ref="helloWorld"/>
    </flow>

我应该怎么做才能将参数 'hello' 传递给 'sayHello()' 方法。如果只是将其更改为 'sayHello(String text)' - 它将不起作用。

【问题讨论】:

  • 什么参数“你好”?你说的是发送“一些数据”:“一些数据”不是你期望传递给sayHello(String text)的吗?
  • 是的,我想传递“一些数据”

标签: esb mule


【解决方案1】:

您需要将此添加到调用元素:

methodArguments="#[message.getPayload()]" methodArgumentTypes="java.lang.String"

【讨论】:

  • 不错的一个!顺便说一句,您宁愿使用 MEL 表达式:"#[message.payload]"。 (附带说明,我仍然更喜欢component 而不是invoke,后者感觉很像“用XML 编程”:P)。
  • 在我输入后: 我有这样的异常:ionException:创建名称为“(内部 bean)”的 bean 时出错:init 方法的调用失败;嵌套异常是 org.mule.api.lifecycle.InitialisationException:单一方法“sayHello”,在 org.mule.registry.AbstractRegistry.initialise 的“org.mule.application.hello.HelloWorld@24be0446”上找不到“0”参数(AbstractRegistry.java:115)
【解决方案2】:

不确定invoke 如何/是否有效:我建议您改用component

如果您将方法更改为接受String,例如:

public String sayHello(final String text)
{
    return "hello:" + text;
}

那么您还需要使用object-to-string-transformer 将入站输入流反序列化为字符串:

<flow name="HelloWorld">
    <inbound-endpoint address="http://127.0.0.1:8080/hello" />
    <object-to-string-transformer />
    <component>
        <spring-object bean="helloWorld" />
    </component>
</flow>

【讨论】:

  • 在此之后,我有: ionException: Error created bean with name '(inner bean)': invocation of init method failed;嵌套异常是 org.mule.api.lifecycle.InitialisationException:单一方法“sayHello”,在 org.mule.registry.AbstractRegistry.initialise 的“org.mule.application.hello.HelloWorld@6d3d7254”上找不到“0”参数(AbstractRegistry.java:115)
  • 在使用client.send("http://&lt;host:port&gt;/hello", "some data", null) 进行测试时对我来说效果很好。你也用过那个命令吗?
  • 对不起,我无法重现您的问题:上述配置对我来说非常适用。你确定你已经在你正在测试的 Mule 上部署了上述配置吗?
  • 我今天将继续努力。稍后会更新这篇文章,会让你知道的。
【解决方案3】:

试试这个:

  • 将此添加到您的流程中:

&lt;invoke object-ref="helloWorld" method="sayHello" methodArguments="#[message.inboundProperties.'http.query.params'.name]" doc:name="Invoke" /&gt;
  • 这是被调用的方法:

    公共字符串sayHello(字符串名称){ return String.format("Hello %s!", name); }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-01
    • 1970-01-01
    • 2014-03-30
    • 1970-01-01
    • 2015-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多