我正在尝试使用选项并根据其属性过滤对象,但它无法访问getter并抛出错误。请帮我找到问题。
代码:
public class SampleTimerRouter extends RouteBuilder {
@Override
public void configure() throws Exception {
from("timer:simpleTimer1?period=2000").process((exchange) -> {
List<TestPOJO> names = new ArrayList<>();
names.add(new TestPOJO("f1"));
names.add(new TestPOJO("f2"));
exchange.getOut().setBody(names);
}).split(body(TestPOJO.class)).choice().when().simple("${body.getfName} == 'f1'").process((exchange) -> {
exchange.getOut().setBody(exchange.getIn().getBody(TestPOJO.class).getfName());
}).to("file:output.txt?fileExist=Append").otherwise().to("stream:out");
}
}
class TestPOJO {
private String fName;
public TestPOJO(String f) {
fName = f;
}
public String getfName() {
return fName;
}
public void setfName(String fName) {
this.fName = fName;
}
}
日志:
Caused by: org.apache.camel.RuntimeExchangeException: IllegalAccessException occurred invoking method: public java.lang.String com.TestPOJO.getfName() using arguments: [] on the exchange: Exchange[]
at org.apache.camel.component.bean.MethodInfo.invoke(MethodInfo.java:474) ~[camel-core-2.19.1.jar:2.19.1]
at org.apache.camel.component.bean.MethodInfo$1.doProceed(MethodInfo.java:291) ~[camel-core-2.19.1.jar:2.19.1]
at org.apache.camel.component.bean.MethodInfo$1.proceed(MethodInfo.java:264) ~[camel-core-2.19.1.jar:2.19.1]
at org.apache.camel.component.bean.BeanProcessor.process(BeanProcessor.java:178) ~[camel-core-2.19.1.jar:2.19.1]
at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:109) ~[camel-core-2.19.1.jar:2.19.1]
at org.apache.camel.component.bean.BeanProcessor.process(BeanProcessor.java:69) ~[camel-core-2.19.1.jar:2.19.1]
at org.apache.camel.language.bean.BeanExpression$InvokeProcessor.process(BeanExpression.java:212) ~[camel-core-2.19.1.jar:2.19.1]
at org.apache.camel.language.bean.BeanExpression$OgnlInvokeProcessor.process(BeanExpression.java:323) ~[camel-core-2.19.1.jar:2.19.1]
... 32 common frames omitted
Caused by: java.lang.IllegalAccessException: Class org.apache.camel.component.bean.MethodInfo can not access a member of class com.TestPOJO with modifiers "public"
at sun.reflect.Reflection.ensureMemberAccess(Unknown Source) ~[na:1.8.0_151]
at java.lang.reflect.AccessibleObject.slowCheckMemberAccess(Unknown Source) ~[na:1.8.0_151]
at java.lang.reflect.AccessibleObject.checkAccess(Unknown Source) ~[na:1.8.0_151]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_151]
at org.apache.camel.component.bean.MethodInfo.invoke(MethodInfo.java:472) ~[camel-core-2.19.1.jar:2.19.1]
... 39 common frames omitted
答案 0 :(得分:1)
虽然方法getfName
为public
,但包含该方法的类TestPOJO
具有仅包访问权限。
因此无法通过Camel代码的反射来访问它(除非它使用setAccessible(true)
,它没有,正如您在问题中所示)。
让TestPOJO public
类解决此问题。