Backport of https://github.com/spring-projects/spring-boot/issues/37118 into 2.7.x, 3.0.x, and 3.1.x.