Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.lang.NoSuchMethodError: ResponseEntity.getStatusCode() when running code built with Spring 5.x on Spring 6.x #29813

Closed
derkoe opened this issue Jan 13, 2023 · 4 comments
Labels
status: invalid An issue that we don't feel is valid

Comments

@derkoe
Copy link

derkoe commented Jan 13, 2023

Affects: 6.0.x - tested with 6.0.4


The signature of ResponseEntity#getStatusCode was changed in Spring Framework 6.x - it now returns HttpStatusCode (Spring 5.x returned HttpStatus) - see #28214. The idea was that the change is backward compatible.

How to reproduce:

  1. Create this simple test class in a file Test.java:

    // Test.java
    import org.springframework.http.ResponseEntity;
    
    public class Test {
      public static void main(String[] args) {
        ResponseEntity<Object> ok = ResponseEntity.ok().build();
        if (ok.getStatusCode().is2xxSuccessful()) {
          System.out.println("OK");
        }
      }
    }
  2. Compile the application with Spring 5.3.x:

    javac -cp $HOME/.m2/repository/org/springframework/spring-web/5.3.24/spring-web-5.3.24.jar Test.java
    
  3. Run the application with Spring 6.0x:

     java -cp "$HOME/.m2/repository/org/springframework/spring-web/6.0.4/spring-web-6.0.4.jar:$HOME/.m2/repository/org/springframework/spring-core/6.0.4/spring-core-6.0.4.jar:." Test
     Exception in thread "main" java.lang.NoSuchMethodError: 'org.springframework.http.HttpStatus org.springframework.http.ResponseEntity.getStatusCode()'
         at Test.main(Test.java:6)
    

    With Spring 5.3.x it works as expected:

     java -cp "$HOME/.m2/repository/org/springframework/spring-web/5.3.24/spring-web-5.3.24.jar:$HOME/.m2/repository/org/springframework/spring-core/5.3.24/spring-core-5.3.24.jar:." Test
    OK
    
@bclozel
Copy link
Member

bclozel commented Jan 13, 2023

This change was done in a source compatible way, but not binary compatible way.
We don't guarantee binary compatibility between major versions, you should recompile the application with Spring Framework 6.0 on the classpath.

@bclozel bclozel closed this as not planned Won't fix, can't repro, duplicate, stale Jan 13, 2023
@bclozel bclozel added status: invalid An issue that we don't feel is valid and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jan 13, 2023
@derkoe
Copy link
Author

derkoe commented Jan 13, 2023

@bclozel the issue is here that you cannot create a library that is Spring 5.x and 6.x compatible. Is this really intended?

@derkoe
Copy link
Author

derkoe commented Jan 13, 2023

This incompatibility also exists in the other direction - code compile with 6.0.x is not running with 5.3.x

$ javac -cp $HOME/.m2/repository/org/springframework/spring-web/6.0.4/spring-web-6.0.4.jar Test.java
$ java -cp "$HOME/.m2/repository/org/springframework/spring-web/5.3.24/spring-web-5.3.24.jar:$HOME/.m2/repository/org/springframework/spring-core/5.3.24/spring-core-5.3.24.jar:." Test
Exception in thread "main" java.lang.NoSuchMethodError: 'org.springframework.http.HttpStatusCode org.springframework.http.ResponseEntity.getStatusCode()'
        at Test.main(Test.java:6)

@bclozel
Copy link
Member

bclozel commented Jan 13, 2023

@derkoe we do our best to retain compatibility, but keeping binary compatibility between major versions is a huge ask.
I'm sorry for the inconvenience, but in this case we think the tradeoff is worth it.

You cannot create a library that is Spring 5.x and 6.x compatible in the first place anyway: they have different Java baselines. If you're compiling your library with Java 17, it's not compatible with all Spring 5.x applications.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

3 participants