Apache Camel:MethodNotFoundException,由于以下原因未能在NULL上调用方法
我尝试遵循用于Apache Camel教育的视频教程,并尝试以下一个方式尝试聚合队列对象:
@Component
public class AggregationRoute extends RouteBuilder {
@Override
public void configure() throws Exception {
from("file:files/json")
.unmarshal().json(JsonLibrary.Jackson, CurrencyExchange.class)
.aggregate(simple("${body.to}"), new ArrayListAggregationStrategy()) // <--- (1)
.completionSize(3)// <----- (2)
.to("log:aggregation");
}
}
Currency Exchange模型:
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class CurrencyExchange {
public int id;
public String from;
public String to;
public BigDecimal conversionMultiple;
}
聚合实现:
public class ArrayListAggregationStrategy implements AggregationStrategy {
@Override
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
Object newObject = newExchange.getIn().getBody();
ArrayList<Object> list = new ArrayList<>();
if(oldExchange == null){
list.add(newObject);
newExchange.getIn().setBody(list);
return newExchange;
} else {
oldExchange.getIn().getBody(list.getClass()).add(newObject);
return oldExchange;
}
}
}
首先 - 该代码在没有(1)和(2)行的情况下正常工作。 当我删除这些行时,我会收到错误:
> 2022-05-12 12:34:26.913 ERROR 82201 --- [le://files/json]
> o.a.c.p.e.DefaultErrorHandler : Failed delivery for
> (MessageId: F948EB377E1DB38-0000000000000000 on ExchangeId:
> F948EB377E1DB38-0000000000000000). Exhausted after delivery attempt: 1
> caught: org.apache.camel.language.bean.RuntimeBeanExpressionException:
> Failed to invoke method: to on null due to:
> org.apache.camel.component.bean.MethodNotFoundException: Method with
> name: to not found on bean:
> com.education.camelmicroservicea.model.CurrencyExchange@301f4095 of
> type: com.education.camelmicroservicea.model.CurrencyExchange on the
> exchange: Exchange[F948EB377E1DB38-0000000000000000]
stacktrace:
> org.apache.camel.language.bean.RuntimeBeanExpressionException: Failed
> to invoke method: to on null due to:
> org.apache.camel.component.bean.MethodNotFoundException: Method with
> name: to not found on bean:
> com.education.camelmicroservicea.model.CurrencyExchange@301f4095 of
> type: com.education.camelmicroservicea.model.CurrencyExchange on the
> exchange: Exchange[F948EB377E1DB38-0000000000000000] at
> org.apache.camel.language.bean.BeanExpression.invokeOgnlMethod(BeanExpression.java:453)
> ~[camel-bean-3.16.0.jar:3.16.0] at
> org.apache.camel.language.bean.BeanExpression.evaluate(BeanExpression.java:199)
> ~[camel-bean-3.16.0.jar:3.16.0] .....
- 请注意,错误消息:
未能调用方法:to on Null上的null:
表示方法to
,因此,听起来可能像无法调用方法:to()
在null上,因此:
为什么simple
通过此例外,如果我收到身体正确?
I try following a video tutorial for Apache Camel education and trying aggregate queue objects in a next way:
@Component
public class AggregationRoute extends RouteBuilder {
@Override
public void configure() throws Exception {
from("file:files/json")
.unmarshal().json(JsonLibrary.Jackson, CurrencyExchange.class)
.aggregate(simple("${body.to}"), new ArrayListAggregationStrategy()) // <--- (1)
.completionSize(3)// <----- (2)
.to("log:aggregation");
}
}
CurrencyExchange model:
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class CurrencyExchange {
public int id;
public String from;
public String to;
public BigDecimal conversionMultiple;
}
Aggregation implementation:
public class ArrayListAggregationStrategy implements AggregationStrategy {
@Override
public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
Object newObject = newExchange.getIn().getBody();
ArrayList<Object> list = new ArrayList<>();
if(oldExchange == null){
list.add(newObject);
newExchange.getIn().setBody(list);
return newExchange;
} else {
oldExchange.getIn().getBody(list.getClass()).add(newObject);
return oldExchange;
}
}
}
First of all - the code works correctly without (1) and (2) lines.
When I uncommented these lines, I get the error:
> 2022-05-12 12:34:26.913 ERROR 82201 --- [le://files/json]
> o.a.c.p.e.DefaultErrorHandler : Failed delivery for
> (MessageId: F948EB377E1DB38-0000000000000000 on ExchangeId:
> F948EB377E1DB38-0000000000000000). Exhausted after delivery attempt: 1
> caught: org.apache.camel.language.bean.RuntimeBeanExpressionException:
> Failed to invoke method: to on null due to:
> org.apache.camel.component.bean.MethodNotFoundException: Method with
> name: to not found on bean:
> com.education.camelmicroservicea.model.CurrencyExchange@301f4095 of
> type: com.education.camelmicroservicea.model.CurrencyExchange on the
> exchange: Exchange[F948EB377E1DB38-0000000000000000]
Stacktrace:
> org.apache.camel.language.bean.RuntimeBeanExpressionException: Failed
> to invoke method: to on null due to:
> org.apache.camel.component.bean.MethodNotFoundException: Method with
> name: to not found on bean:
> com.education.camelmicroservicea.model.CurrencyExchange@301f4095 of
> type: com.education.camelmicroservicea.model.CurrencyExchange on the
> exchange: Exchange[F948EB377E1DB38-0000000000000000] at
> org.apache.camel.language.bean.BeanExpression.invokeOgnlMethod(BeanExpression.java:453)
> ~[camel-bean-3.16.0.jar:3.16.0] at
> org.apache.camel.language.bean.BeanExpression.evaluate(BeanExpression.java:199)
> ~[camel-bean-3.16.0.jar:3.16.0] .....
- Please note, that the error message:
Failed to invoke method: to on null due to:
means methodto
, so it can sound like Failed to invoke method:to()
on null due to:
Why the simple
through this exception, if I receive the body in the correct way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您使用反射时,您只是不需要信任Lombok。
在我方便地添加getter之后,一切都很好:
You just don't need to trust Lombok when u use reflection.
After I added getter handly, all works fine: