ses
ses

Reputation: 13372

esb mule passing the parameters to the method via http

I have a test method:

@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());
    }

Here (client.send("http://127.0.0.1:8080/hello", "some data", null)), I'm passing the parameter/data = 'some data'.

And I have a class:

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

which is exposed as spring bean in mule-config.xml:

<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>

What I should do to pass the parameter 'hello' into the 'sayHello()' method. If just changing it to 'sayHello(String text)' - it will not work.

Upvotes: 1

Views: 2867

Answers (3)

Mohamed AbdElRazek
Mohamed AbdElRazek

Reputation: 1684

Try This :

  • add this in your flow :

<invoke object-ref="helloWorld" method="sayHello" methodArguments="#[message.inboundProperties.'http.query.params'.name]" doc:name="Invoke" />

  • and this is the invoked method :

    public String sayHello(String name) { return String.format("Hello %s!", name); }

Upvotes: 0

David Dossot
David Dossot

Reputation: 33413

Not sure about how/if invoke works: I suggest you use component instead.

If you change your method to accept a String, like for example:

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

then you also need to use an object-to-string-transformer to deserialize the inbound input stream to a String:

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

Upvotes: 0

Ale Sequeira
Ale Sequeira

Reputation: 2039

You need to add this to the invoke element:

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

Upvotes: 4

Related Questions