I am trying to get a method-entry-point-resolver to cast the payload to a base class, as indicated by the argument of the requested method. However, Mule is not doing that. What could I be doing wrong?
That is, given the following configuration:
<mule ...>
<spring:bean id="FooBean" class="foo.Foo" />
<flow name="test">
<vm:inbound-endpoint name="test.Name" path="test.Path" exchange-pattern="request-response" />
<component>
<method-entry-point-resolver>
<include-entry-point method="bar" />
</method-entry-point-resolver>
<spring-object bean="FooBean" />
</component>
</flow>
</mule>
and given foo.Foo:
package foo;
public class Foo {
public Foo() {
}
public String bar(final Object anObject) {
return "bar";
}
}
I would expect the following test to pass, but it does not. That is, the payload that I am sending to the flow is an Integer and I am expecting Mule to pass it as an argument to Foo::bar:
@Test
public void methodEntryPointResolverUpcasts() throws MuleException {
final MuleClient client = muleContext.getClient();
final MuleMessage reply = client.send("vm://test.Path", new Integer(1), null, RECEIVE_TIMEOUT);
assertEquals("bar", reply.getPayload());
}
Instead, the logs show an error. Here is a relevant snippet:
...
Message : Failed to find entry point for component, the following resolvers tried but failed: [
ExplicitMethodEntryPointResolver: Could not find entry point on: "foo.Foo" with arguments: "{class java.lang.Integer}"
]
...
Exception stack is:
1. Failed to find entry point for component, the following resolvers tried but failed: [
ExplicitMethodEntryPointResolver: Could not find entry point on: "foo.Foo" with arguments: "{class java.lang.Integer}"
Mule’s entry point resolvers don’t perform casting per se: they look for methods that could potentially accept a particular payload.
This said, the
method-entry-point-resolverrequires strict type matching to work. Behind the scene, we find in ExplicitMethodEntryPointResolver.java, the class that backs it, the following line:The
falsethere means: do not match on Object. This is the reason why matching doesn’t work for you. Unfortunately this is not configurable.When you remove the explicit configuration of an entry point resolver, Mule uses a default chain of resolvers that contains the
reflection-entry-point-resolver. This is the one that happily passes an Integer into the Object argument, because in ReflectionEntryPointResolver.java:The second true means: match on object!
So if you want to specify a single entry point resolver in your config, the
reflection-entry-point-resolveris your friend 🙂