jspetrak
jspetrak

Reputation: 103

Calling method A is forbidden for type B in Thymeleaf expressions

I have just upgraded web project to Spring 6, Spring Boot 3 GA, and now I deal with following Thymeleaf incompability. I am generating form select.

<select class="form-select" th:field="*{bankAccountId}" th:classappend="${#fields.hasErrors('bankAccountId')} ? 'is-invalid'">
   <option value="" th:text="#{SelectBankAccount}">SELECT_BANK_ACCOUNT</option>
   <option th:each="bankAccount : ${bankAccounts}" th:value="${bankAccount.getId()}" th:text="${bankAccount.getName()}" th:selected="${bankAccount.getId().equals(bankAccountId)}">BANK_ACCOUNT_NAME</option>
</select>

Expression in th:selected is failing with following exception.

org.springframework.expression.EvaluationException: Calling method 'equals' is forbidden for type 'class java.util.UUID' in Thymeleaf expressions. Blocked classes are: [com.sun.*, jakarta.*, java.*, javax.*, jdk.*, org.ietf.jgss.*, org.omg.*, org.w3c.dom.*, org.xml.sax.*, sun.*]. Allowed classes are: [java.lang.Boolean, java.lang.Byte, java.lang.Character, java.lang.Double, java.lang.Enum, java.lang.Float, java.lang.Integer, java.lang.Iterable, java.lang.Long, java.lang.Math, java.lang.Number, java.lang.Short, java.lang.String, java.math.BigDecimal, java.math.BigInteger, java.math.RoundingMode, java.time.*, java.util.ArrayList, java.util.Calendar, java.util.Calendar, java.util.Collection, java.util.Date, java.util.Enumeration, java.util.HashMap, java.util.HashSet, java.util.Iterator, java.util.LinkedHashMap, java.util.LinkedHashSet, java.util.LinkedList, java.util.List, java.util.Locale, java.util.Map, java.util.Map$Entry, java.util.Optional, java.util.Properties, java.util.Set, java.util.stream.Stream].
    at org.thymeleaf.spring6.expression.ThymeleafEvaluationContext$ThymeleafEvaluationContextACLMethodResolver.resolve(ThymeleafEvaluationContext.java:282) ~[thymeleaf-spring6-3.1.0.RELEASE.jar:3.1.0.RELEASE]

Both bankAccount.getId() and $bankAccountId are java.util.UUID.

Cast to String is not possible in Thymeleaf expression. All methods calls on java.util package objects are forbidden.

Not sure what is the most correct workaround. Either I can cast the UUIDs to Strings in backAccount object, add comparator method into backAccount, call a comparator util statically or via @bean notation, or even define a custom method for Thymeleaf and use as #xxx() expression.

Upvotes: 1

Views: 2301

Answers (1)

Christoph Derszteler
Christoph Derszteler

Reputation: 145

Had the same issue, however it was a bug with Spring. It was resolved in the version 3.0.1.

For more information, look here: https://github.com/thymeleaf/thymeleaf-spring/issues/200#issuecomment-545922604

Upvotes: 1

Related Questions