Search code examples
javaspringspring-bootspring-security

Spring boot 3 Upgrade - java.lang.NoSuchMethodError


Our app is being migrated to Spring boot 3 and spring 6. We see the error : "java.lang.NoSuchMethodError: 'org.springframework.http.HttpStatus org.springframework.http.client.ClientHttpResponse.getStatusCode()" when testing with a request .

There are no compile errors and maven build was fine.

Used this as a reference : java.lang.NoSuchMethodError: 'org.springframework.http.HttpStatus org.springframework.http.client.ClientHttpResponse.getStatusCode()'

and verified org.springframework:spring-web:jar:6.1.10 is the version that is used (from the dependency tree)

The .factoryPath in eclipse alone has a reference for older version (<factorypathentry kind="VARJAR" id="M2_REPO/org/springframework/spring-web/5.3.29/spring-web-5.3.29.jar" enabled="true" runInBatchMode="false"/> ) .. In the dependency tree I do not see the reference for older spring-web.

Any pointers for the upgrade error ?

Other jars used :

spring-security-oauth2:jar:2.5.1.RELEASE
spring-webmvc:jar:6.1.10
spring-security-core:jar:6.2.5
spring-boot-starter:jar:3.2.7
spring-security-oauth2-client:jar:6.2.5

Code working as expected


Solution

  • The root cause for this error is that somehow, in your class path, an already compiled binary is trying to invoke .getStatusCode() from ClientHttpResponse from spring framework 5. This class has been changed in spring framework 6 and spring boot 3, so that it returns HttpStatusCode instead of HttpStatus of previous implementation. The binary file is expecting to call a method signature of HttpStatus getStatusCode() but the runtime environment has in classpath the method signature from spring framework 6 defined as HttpStatusCode getStatusCode(). Hence the exception of no such method is raised. Here is a previous answer to help you understand why the difference in return type makes the runntime throw the no such method error.

    Locate the exact place where the stack trace originates from and investigate the binaries. Most probably you will find that some .class file is not compiled with the new spring-framework version.

    Edit: Basically the incompatibility that causes the above could as well be defined in your question. spring-security-oauth2:jar:2.5.1.RELEASE is not compatible with spring framework 6 and spring boot 3. This jar file is already compiled with spring framework 5.

    You should migrate the code and replace your dependency spring-security-oauth2:jar:2.5.1.RELEASE with e.g. spring-security-oauth2-client:6.3.3 and/or spring-security-oauth2-resource-server:6.3.3 which are compiled with spring framework 6.