'Apache Camel: MethodNotFoundException, Failed to invoke method on null due to: MethodNotFoundException

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?



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Valentyn Hruzytskyi