【问题标题】:Unable to run Spring Boot app after upgrading to 2.2.4: method does not exist升级到 2.2.4 后无法运行 Spring Boot 应用:方法不存在
【发布时间】:2020-02-06 05:23:34
【问题描述】:

将 Spring Boot 版本从 2.1.11 升级到 2.2.4 后出现错误。然后我把 spring-cloud-starter-openfeign 从 2.0.2 升级到了 2.2.1,但是没有用。

以下方法不存在:

'org.springframework.boot.web.client.RestTemplateBuilder org.springframework.boot.web.client.RestTemplateBuilder.setConnectTimeout(int)'

该方法的类 org.springframework.boot.web.client.RestTemplateBuilder 可从以下位置获得:

jar:file:/C:/Users/a/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot/2.2.4.RELEASE/225a4fd31156c254e3bb92adb42ee8c6de812714/spring-boot-2.2.4.RELEASE.jar!/org/springframework/boot/web/client/RestTemplateBuilder.class

它是从以下位置加载的:

file:/C:/Users/a/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot/2.2.4.RELEASE/225a4fd31156c254e3bb92adb42ee8c6de812714/spring-boot-2.2.4.RELEASE.jar

调用方法的尝试发生在自定义类的构造函数中

 this.restTemplate = restTemplateBuilder.setConnectTimeout(props.getConnectTimeoutMs()).setReadTimeout(props.getReadTimeoutMs()).build();

build.gradle:

buildscript {
    repositories {...
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.2.4.RELEASE")
        classpath('gradle.plugin.com.palantir.gradle.docker:gradle-docker:0.17.2')
        classpath('org.unbroken-dome.gradle-plugins:gradle-testsets-plugin:2.2.1')
    }
}

plugins {
    id "org.sonarqube" version "2.6.2"
    id "com.jfrog.artifactory" version "4.6.2"
}

apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'idea'
apply plugin: 'jacoco'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'com.palantir.docker'
apply plugin: 'org.unbroken-dome.test-sets'

sourceCompatibility = 11

dependencyManagement {
    imports {
        mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Hoxton.RELEASE'
    }
}

configurations.all {
    exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
    exclude group: 'ch.qos.logback", module: "logback-classic'
    exclude group: 'ch.qos.logback", module: "logback-core'
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter')
    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('javax.xml.bind:jaxb-api:2.3.0')
    compile('org.springframework.boot:spring-boot-starter-hateoas')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.cloud:spring-cloud-starter-openfeign:2.2.1.RELEASE')
    compile('io.github.openfeign:feign-jackson:10.7.4')
    compile("org.togglz:togglz-spring-boot-starter:2.6.1.Final")
    compile('org.springframework.retry:spring-retry:1.2.2.RELEASE')
    runtime('io.micrometer:micrometer-registry-prometheus:1.3.2')
    compileOnly("org.projectlombok:lombok:1.18.6")
    annotationProcessor("org.projectlombok:lombok:1.18.6")
    compile("org.mapstruct:mapstruct:1.3.0.Final")
    annotationProcessor("org.mapstruct:mapstruct-processor:1.3.0.Final")
    compile('com.microsoft.azure:azure-servicebus:1.2.12')
    compile("io.springfox:springfox-swagger2:2.9.2")
    compile("io.springfox:springfox-swagger-ui:2.9.2")
    compile('org.apache.commons:commons-io:1.3.2')
    compile("com.fasterxml.jackson.module:jackson-module-parameter-names:2.10.2")
    compile("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.10.2")
    compile('org.apache.commons:commons-lang3:3.7')
    compile('commons-validator:commons-validator:1.6')
    compile('org.apache.httpcomponents:httpclient:4.5.8')
    compile('org.springframework.boot:spring-boot-starter-log4j2')
    testCompile('org.testcontainers:spock:1.12.4')
    testCompile('org.codehaus.groovy:groovy-all:2.5.8')
    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile("org.spockframework:spock-core:1.3-groovy-2.5")
    testCompile("org.spockframework:spock-spring:1.3-groovy-2.5")
    testCompile('nl.jqno.equalsverifier:equalsverifier:3.1.10')
    compile('com.microsoft.azure:azure:1.20.1')
    testAnnotationProcessor("org.projectlombok:lombok:1.18.6")
    testCompile('com.github.tomakehurst:wiremock:2.15.0')
    testCompile('com.github.tomjankes:wiremock-groovy:0.2.0')
    testCompile('com.rabbitmq:amqp-client:5.2.0')
}

【问题讨论】:

    标签: spring-boot gradle build.gradle resttemplate


    【解决方案1】:

    分别查看2.1.112.2.4 版本的Javadoc。旧版本仍然公开此方法采用 int 参数,但已经声明它已被弃用,您应该改用 java.time.Duration

    接受java.time.Duration 的方法在 2.2.4 中仍然存在,您应该改用那个方法。

    【讨论】:

      【解决方案2】:

      似乎setConnectTimeout(int connectTimeout) 已被弃用,而是添加了setConnectTimeout(Duration connectTimeout)。请尝试添加此

      【讨论】:

      • 感谢您的回答!它解决了我的问题,就像 wjans 之前的回答一样
      • 如果您觉得有帮助,请投票。几乎两个人同时回答
      • 我希望可以。不幸的是,我没有足够的声誉:(
      猜你喜欢
      • 2021-01-18
      • 2018-10-31
      • 2022-01-23
      • 2022-01-22
      • 2020-05-22
      • 1970-01-01
      • 2015-12-14
      • 2021-08-13
      • 2022-06-24
      相关资源
      最近更新 更多