I tried to use EL to access the Optional<PhoneNubmer> in a record.
public record PhoneNumber(String countryCode, String number) {
}
And the Customer:
public record Customer(
String firstName,
String lastName,
Optional<PhoneNumber> phoneNumber,
EmailAddress[] emailAddresses,
Address address
) {
}
And evaluate the following expression.
String phoneNumber = elProcessor.eval("customer.phoneNumber.map(t-> t.countryCode+t.number).orElse('NotFound')");
System.out.println(phoneNumber);
It throws an exception,
java.lang.ClassCastException: class java.lang.Long cannot be cast to
class java.lang.String (java.lang.Long and java.lang.String are in
module java.base of loader 'bootstrap')
But there is no Long type field in EL expression.
As suggested by @OndroMih, it is better to show a message to indicate what is wrong, which will help developers to locate the issues quickly.
customer.phoneNumber.map(t-> t.countryCode[>>> + ]t.number).orElse('NotFound')
BTW: Replace the map() function with map(t -> t.contryCode.concat(t.number)), and it works.
I tried to use EL to access the
Optional<PhoneNubmer>in a record.And the
Customer:And evaluate the following expression.
It throws an exception,
But there is no
Longtype field in EL expression.As suggested by @OndroMih, it is better to show a message to indicate what is wrong, which will help developers to locate the issues quickly.
BTW: Replace the
map()function withmap(t -> t.contryCode.concat(t.number)), and it works.