Reputation: 220
I am upgrading my springboot application from springboot 2.9.7 to springboot 3.0.2. While doing clean build, I am getting below error related to Rest API calls (using Rest template).
Caused by: java.lang.NoSuchMethodError: 'org.springframework.http.HttpStatus org.springframework.http.ResponseEntity.getStatusCode()'
As per my understanding it has something to do with upgarde happening to springframework to some 6.. version due to upgrade in springboot version to 3.0.2.
Anyone has faced this issue? how this can be fixed.
It's a gradle project. I have added below configuration related to spring boot upgrade.
springBootVersion = '3.0.2'
Other than that , its a simple spring boot microservice where we have one endpoint, which calls internally another Rest API etc.
I have only changed spring boot version from 2.9.7 to springboot 3.0.2, and it started to fail with above mentioned error.
Upvotes: 8
Views: 9403
Reputation: 99
Verify that your spring-web version is compatible with spring-boot. Exclude old version from transitive dependency. reference: https://stackoverflow.com/a/75577453/2451814
Upvotes: 0
Reputation: 1727
Spring Boot 3 builds on and requires Spring Framework 6.0 version so in Spring framework 6.0 there is code change related to HttpStatus.
As mentioned in above comment, getStatusCode of ResponseEntity should return HttpStatusCode instead of HttpStatus. Then we need to call value method to get the integer code.
HttpStatusCode statusCode = responseEntity.getStatusCode();
int status = statusCode.value();
Reference Spring Doc
Upvotes: 1
Reputation: 1837
Caused by: java.lang.NoSuchMethodError: 'org.springframework.http.HttpStatus
org.springframework.http.ResponseEntity.getStatusCode()'
Error says that there is no such method getStatusCode()
which returns HttpStatus
. And that is correct, because this method in new version of Spring returns HttpStatusCode
instead.
// HttpStatus status = responseEntity.getStatusCode();
// Should be ↓
HttpStatusCode statusCode = responseEntity.getStatusCode();
int statusCodeValue = responseEntity.getStatusCode().value();
Changes: spring-projects/spring-framework
Upvotes: 9