我正在尝试让方法入口点解析器将有效负载转换为基类,如所请求方法的参数所示。然而,骡子并没有这样做。我可能做错了什么?
也就是说,给定以下配置:
<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>
并给予福。福:
package foo;
public class Foo {
public Foo() {
}
public String bar(final Object anObject) {
return "bar";
}
}
我希望以下测试能够通过,但它没有。也就是说,我发送到流的有效负载是一个Integer
,我希望 Mule 将其作为参数传递给 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());
}
相反,日志显示错误。这是一个相关的片段:
...
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 的入口点解析器本身不执行强制转换:它们寻找可能接受特定有效负载的方法。
也就是说,method-entry-point-resolver
需要严格的类型匹配才能工作。在幕后,我们在 ExplicitMethodEntryPointResolver.java(支持它的类)中找到以下行:
if (ClassUtils.compare(parameterTypes, classTypes, false, true))
那里的false
意味着:在对象上不匹配。这就是匹配不适合您的原因。不幸的是,这是不可配置的。
删除入口点解析程序的显式配置时,Mule 将使用包含 reflection-entry-point-resolver
的默认解析程序链。这是愉快地将整数传递到 Object 参数中的那个,因为在 ReflectionEntryPointResolver.java:
methods = ClassUtils.getSatisfiableMethods(component.getClass(),
ClassUtils.getClassTypes(payload), true, true, ignoredMethods);
第二个真的意思是:匹配对象!
因此,如果您想在配置中指定单个入口点解析器,则reflection-entry-point-resolver
是您的朋友:)