Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot 3 upgrade error : 'org.springframework.http.HttpStatus org.springframework.http.ResponseEntity.getStatusCode()'

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.

like image 204
Sachin Mankotia Avatar asked May 19 '26 15:05

Sachin Mankotia


2 Answers

Reading error

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.

Example

// HttpStatus status = responseEntity.getStatusCode();
// Should be ↓

HttpStatusCode statusCode = responseEntity.getStatusCode();
int statusCodeValue = responseEntity.getStatusCode().value();

Changes: spring-projects/spring-framework

like image 54
Dumbo Avatar answered May 21 '26 04:05

Dumbo


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

like image 41
Anil Nivargi Avatar answered May 21 '26 04:05

Anil Nivargi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!