Apache Camel:MethodNotFoundException,由于以下原因未能在NULL上调用方法

发布于 2025-01-28 02:53:28 字数 3243 浏览 2 评论 0原文

我尝试遵循用于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 method to, 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往事随风而去 2025-02-04 02:53:28

当您使用反射时,您只是不需要信任Lombok。

在我方便地添加getter之后,一切都很好:

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class CurrencyExchange {
    private int id;
    private String from;
    private String to;
    private BigDecimal conversionMultiple;

    public String getTo(){
        return this.to;
    }
}

You just don't need to trust Lombok when u use reflection.

After I added getter handly, all works fine:

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class CurrencyExchange {
    private int id;
    private String from;
    private String to;
    private BigDecimal conversionMultiple;

    public String getTo(){
        return this.to;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文