SpringBoot 3.1.3升级后HTTP调用报NoSuchMethodError问题求助
SpringBoot升级至3.1.3版本后,应用启动正常,但发起HTTP调用时出现NoSuchMethodError错误:
jakarta.servlet.ServletException: Handler dispatch failed: java.lang.NoSuchMethodError: 'org.springframework.http.HttpStatus org.springframework.http.client.ClientHttpResponse.getStatusCode()'
Caused by: java.lang.NoSuchMethodError: 'org.springframework.http.HttpStatus org.springframework.http.client.ClientHttpResponse.getStatusCode()'
at org.springframework.security.oauth2.client.http.OAuth2ErrorHandler.hasError(OAuth2ErrorHandler.java:81)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:904)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:864)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:764)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:646)No candidates found for method call response.getStatusCode().series()
已完成javax到jakarta的迁移,但无法定位缺失的依赖,附上build.gradle依赖配置:
dependencies { classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}" classpath "org.ajoberstar:gradle-git:1.7.2" } plugins { id 'idea' id 'io.spring.dependency-management' version '1.1.3' id 'java' id 'eclipse' id 'maven-publish' id 'org.springframework.boot' version '3.1.3' id 'checkstyle' id 'org.ajoberstar.grgit' version '1.7.2' } apply plugin: org.ajoberstar.gradle.git.base.GrgitPlugin configurations.all { resolutionStrategy.eachDependency { DependencyResolveDetails details -> if (details.requested.group == 'org.apache.logging.log4j') { details.useVersion '2.17.1' } else if (details.requested.group == 'com.fasterxml.jackson.core') { details.useVersion '2.14.1' } else if (details.requested.group == 'com.google.code.gson') { details.useVersion '2.10.1' } else if (details.requested.name == 'snakeyaml') { details.useVersion '1.33' } } } dependencies { implementation 'jakarta.xml.ws:jakarta.xml.ws-api:2.3.3' implementation 'com.sun.xml.bind:jaxb-impl:4.0.3' implementation 'javax.servlet:javax.servlet-api:4.0.1' implementation 'com.google.code.gson:gson:2.10.1' implementation 'com.github.blagerweij:liquibase-sessionlock:1.6.0' implementation "com.fasterxml.jackson.core:jackson-databind:2.14.1" implementation 'io.netty:netty-codec-http:4.1.97.Final' implementation 'com.fasterxml.woodstox:woodstox-core:6.4.0' implementation "org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0" implementation 'org.springdoc:springdoc-openapi-starter-common:2.2.0' implementation 'org.hibernate:hibernate-core:6.2.7.Final' implementation 'org.springframework.security:spring-security-crypto:5.6.4' implementation group: 'org.springframework', name: 'spring-webmvc' implementation 'ch.qos.logback:logback-core' implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.6.0' implementation 'com.amazonaws:aws-java-sdk:1.12.261' implementation 'org.springframework:spring-beans' implementation 'org.jetbrains:annotations:13.0' implementation("org.springframework.boot:spring-boot-starter:${springBootVersion}"){ exclude group:'org.springframework.boot', module:'spring-boot-starter-logging' } implementation "org.springframework.boot:spring-boot-starter-web:${springBootVersion}" implementation "org.springframework.boot:spring-boot-starter-validation:${springBootVersion}" implementation("org.springframework.boot:spring-boot-starter-data-jpa:${springBootVersion}") { exclude module: 'xml-apis' } implementation "org.springframework.boot:spring-boot-starter-security:${springBootVersion}" implementation "org.springframework.boot:spring-boot-starter-actuator:${springBootVersion}" implementation 'com.google.guava:guava:30.0-jre' implementation 'org.springframework.cloud:spring-cloud-aws-context:2.2.5.RELEASE' implementation group: 'com.amazonaws', name: 'aws-java-sdk-ses', version: '1.11.128' implementation 'org.apache.commons:commons-lang3:3.12.0' implementation group: 'javax.ws.rs', name: 'javax.ws.rs-api', version: '2.0' compileOnly 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok' testCompileOnly 'org.projectlombok:lombok' testAnnotationProcessor 'org.projectlombok:lombok' implementation 'com.sun.xml.ws:jaxws-rt:4.0.0' implementation group: 'org.springframework.security.oauth', name: 'spring-security-oauth2', version: '2.5.2.RELEASE' implementation 'org.apache.tomcat.embed:tomcat-embed-core' runtimeOnly "com.squareup.okhttp3:okhttp:5.0.0-alpha.11" implementation 'com.mysql:mysql-connector-j:8.1.0' implementation group: 'com.jcraft', name: 'jsch', version: '0.1.54' implementation group: 'commons-io', name: 'commons-io', version: '2.7' implementation group: 'commons-beanutils', name: 'commons-beanutils', version: '1.9.4' implementation group: 'com.amazonaws', name: 'aws-java-sdk-datapipeline', version: '1.11.526' implementation 'org.springframework.ws:spring-ws-core' implementation group: 'com.amazonaws', name: 'aws-java-sdk-dynamodb', version: '1.11.373' implementation group: 'io.prometheus', name: 'simpleclient', version: '0.16.0' implementation group: 'io.prometheus', name: 'simpleclient_hotspot', version: '0.16.0' implementation group: 'io.prometheus', name: 'simpleclient_servlet', version: '0.16.0' implementation group: 'io.prometheus', name: 'simpleclient_spring_web', version: '0.16.0' implementation group: 'com.opencsv', name: 'opencsv', version: '4.4' implementation group: 'org.apache.pdfbox', name: 'pdfbox', version: '1.8.16' implementation group: 'org.aspectj', name: 'aspectjweaver', version: '1.9.20' implementation group: 'com.stripe', name: 'stripe-java', version: '22.7.0' implementation group: 'com.nimbusds', name: 'nimbus-jose-jwt', version: '9.15.2' runtimeOnly 'org.liquibase:liquibase-core:4.18.0' testImplementation('org.springframework.boot:spring-boot-starter-test'){ exclude group: 'org.junit.vintage', module: 'junit-vintage-engine' } testImplementation 'ch.qos.logback:logback-classic' testImplementation 'com.google.code.findbugs:jsr305:3.0.2' testImplementation 'org.mockito:mockito-core:2.10.0' testImplementation('org.springframework.security:spring-security-test') testRuntimeOnly 'com.h2database:h2:2.1.214' testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc' testImplementation group: 'org.powermock', name: 'powermock-api-mockito2', version: '2.0.7' testImplementation group: 'org.junit.platform', name: 'junit-platform-runner', version: '1.6.2' }
核心问题
错误源于依赖版本不兼容:
- Spring Boot 3.1.3依赖Spring Framework 6.0.x,该版本中
ClientHttpResponse.getStatusCode()方法返回值已从org.springframework.http.HttpStatus改为org.springframework.http.HttpStatusCode - 你使用的
spring-security-oauth2:2.5.2.RELEASE基于Spring Framework 5.x开发,调用旧版本方法导致找不到错误
此外还有其他版本冲突点:
spring-security-crypto:5.6.4与Spring Boot 3.1.3依赖的Spring Security 6.1.x不兼容javax.servlet:javax.servlet-api:4.0.1未迁移到Jakarta版本,Spring Boot 3.x仅支持Jakarta EE APIorg.springframework.cloud:spring-cloud-aws-context:2.2.5.RELEASE为旧版本,不兼容Spring Boot 3.x
修复步骤
替换OAuth2依赖:
移除旧的spring-security-oauth2,改用Spring Boot 3.x兼容的OAuth2 starter:// 移除 // implementation group: 'org.springframework.security.oauth', name: 'spring-security-oauth2', version: '2.5.2.RELEASE' // 添加 implementation "org.springframework.boot:spring-boot-starter-oauth2-client:${springBootVersion}"统一Spring Security版本:
移除单独指定的spring-security-crypto,使用Spring Boot starter管理的版本:// 移除 // implementation 'org.springframework.security:spring-security-crypto:5.6.4'完成Jakarta迁移:
将javax.servlet-api替换为Jakarta版本(Spring Boot web starter已包含,可直接移除旧依赖):// 移除 // implementation 'javax.servlet:javax.servlet-api:4.0.1'升级Spring Cloud AWS依赖:
替换为兼容Spring Boot 3.x的版本:// 移除 // implementation 'org.springframework.cloud:spring-cloud-aws-context:2.2.5.RELEASE' // 添加 implementation 'io.awspring.cloud:spring-cloud-aws-starter:3.0.2'清理冗余依赖:
- 移除
org.apache.tomcat.embed:tomcat-embed-core,spring-boot-starter-web已包含该依赖 - 移除
org.springframework:spring-webmvc和org.springframework:spring-beans,Spring Boot starters已包含核心依赖
- 移除
验证依赖树:
执行Gradle命令检查依赖冲突:./gradlew dependencies --configuration compileClasspath重点排查
org.springframework、org.springframework.security相关依赖,确保无5.x版本混入
内容的提问来源于stack exchange,提问作者Shena Luca

