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

3

6

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.

Scyphus answered 22/5, 2023 at 8:3 Comment(3)
Probably you are mixing jars from different versions of a framework, bu trying to outsmart the Spring Boot (starters) dependency management. Add the full error / stacktrace and add your build file to the question.Reld
Please include the gradle file and a snippet of the relevant class showing the failing line and all the imports to the questionBenfield
Can you share the relevant parts of the build.gradle file including all Spring dependencies and related versions?Vassell
N
8

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

Necrophilia answered 19/3 at 13:7 Comment(0)
H
0

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

Humorist answered 23/3 at 12:17 Comment(0)
M
0

Verify that your spring-web version is compatible with spring-boot. Exclude old version from transitive dependency. reference: https://mcmap.net/q/941426/-java-lang-nosuchmethoderror-39-org-springframework-http-httpstatus-org-springframework-http-client-clienthttpresponse-getstatuscode-39-duplicate

Mucous answered 25/3 at 14:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.