thymeleaf-spring icon indicating copy to clipboard operation
thymeleaf-spring copied to clipboard

spring boot 4, iterable of objects empty in template

Open pnoe opened this issue 2 months ago • 3 comments

Hi, I am trying to upgrade a spring boot mvc tymeleaf application from spring boot 3.5.8 to spring boot 4.0.0.

  • jdk 25, tomcat 11, ubuntu 25.10

In a controller :

Iterable<RucherMap> ruchers;
 ruchers = rucherRepository.findRucherMapByActifTrue();
 List<String> nomRuches = new ArrayList<>();
 for (RucherMap rucher : ruchers) {
   nomRuches.add(rucheRepository.findByRucherId(rucher.id()).stream().map(Nom::nom)
	.reduce("", (a, b) -> a + " " + b).trim());
 }
 model.addAttribute(Const.RUCHENOMS, nomRuches);
 model.addAttribute(Const.RUCHERS, ruchers);
 return "templateThymeleaf";

In java debug mode everything is ok (identical spring boot 3 or 4) ruchers list of objects of type RucherMap nomRuches list of strings

But in the Thymeleaf template

<script th:inline="javascript">
  const ruchers = /*[[${ruchers}]]*/ null;
  const nomRuches = /*[[${rucheNoms}]]*/ null;
  ...

Source code of the html template page : Spring boot 3 :

	const ruchers = [{"id":2623,"depot":false,"nom":"Riez-272","longitude":6.0708833,"latitude":43.80421,"altitude":569,"dessin":null},{"id":414,"depot":false,"nom":"Relais St Ser","longitude":5.64433,"latitude":43.529827,"altitude":429,"dessin":null}, ...
	const nomRuches = ["","FG JC LK DR FU SC LI SJ BE GV SB","","JG JW KG EF GM ES KD GX DS GE GR", ...

Spring boot 4 : ``` const ruchers = [{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}]; const nomRuches = ["","FG JC LK DR FU SC LI SJ BE GV SB","","JG JW KG EF GM ES KD GX DS GE GR", ...


The iterable "ruchers" contains empty objects.
"nomRuches" list of strings is ok.

I have no error in java log, and no error in javascript console.


Thanks for your help.

Pierre.

pnoe avatar Nov 22 '25 13:11 pnoe

In the same page if I add <span th:text="${ruchers}"></span> Then the iterable "ruchers" display is fine : [RucherMap[id=2623, depot=false, nom=Riez-272, longitude=6.0708833,...

It appears that the problem is only for th:inline javascript.

pnoe avatar Nov 24 '25 09:11 pnoe

As a workaround, doing the json mapping in java is working :

final ObjectMapper objectMapper = JsonMapper.builder().build();
model.addAttribute(Const.RUCHERS, objectMapper.writeValueAsString(ruchers));

pnoe avatar Nov 26 '25 14:11 pnoe

Or rather add Jackson 2 to maven pom :

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-jackson2</artifactId>
</dependency>

as documented here :

https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-4.0-Migration-Guide#jackson-2-compatibility

pnoe avatar Nov 27 '25 07:11 pnoe