Apache Camel :MethodNotFoundException,无法对空值调用方法,原因是:未找到方法异常错误

xggvc2p6  于 2022-11-07  发布在  Apache
关注(0)|答案(1)|浏览(140)

我试着按照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");
    }
}

货币兑换模型:

@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]

堆栈跟踪:

> 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] .....
  • 请注意,错误消息:Failed to invoke method: to on null due to:表示方法to,因此听起来可能像调用方法失败:to()为空,原因是:

为什么simple通过这个异常,如果我以正确的方式接收正文呢?

i86rm4rw

i86rm4rw1#

当你使用反射时,你不需要相信Lombok。
我加了消气剂后,一切正常:

@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;
    }
}

相关问题