One can run into this error despite using Java version below JDK 21 for the project if the Project settings in IntelliJ was using JDK 21 to compile.
Here's an example:
The pom.xml has java level set to 17
The Module Settings show the level as 17
But the Project setting picked up the JDK 21 installed on my machine
Change the SDK to use JDK 17 and it should work fine