android - java.lang.NoClassDefFoundError : org. springframework.web.client.RestTemplate 错误

标签 android spring-boot resttemplate spring-android

当我在我的 android 应用程序中使用 resttemplate 时出现以下错误。当我在我的代码中使用 resttemplate 时出现此错误。

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.lss.loop, PID: 17285
              java.lang.NoClassDefFoundError: org.springframework.web.client.RestTemplate
                  at com.esp.office.services.ServerAuthenticateService.<init>(ServerAuthenticateService.java:240)
                  at com.esp.office.authentication.AccountGeneral.<clinit>(AccountGeneral.java:30)
                  at com.esp.office.view.LoginActivity.onCreate(LoginActivity.java:138)
                  at android.app.Activity.performCreate(Activity.java:5459)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2458)
                  at android.app.ActivityThread.access$900(ActivityThread.java:172)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1305)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:146)
                  at android.app.ActivityThread.main(ActivityThread.java:5598)
                  at java.lang.reflect.Method.invokeNative(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:515)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
                  at dalvik.system.NativeStart.main(Native Method)

下面是代码。

this.restTemplate = new RestTemplate();
    List<HttpMessageConverter<?>> messageConverters = new ArrayList();
    messageConverters.add(new StringHttpMessageConverter());
    messageConverters.add(new FormHttpMessageConverter());
    this.restTemplate.setMessageConverters(messageConverters);
    this.restTemplate.setErrorHandler(new C07681());
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(Date.class, new C07692());
    builder.registerTypeAdapter(Date.class, new C07703());
    builder.registerTypeAdapter(Timestamp.class, new C07714());
    builder.registerTypeAdapter(byte[].class, new C07725());
    this.gson = builder.create();

插入数据后的异常,我调试并发现异常是为了这个:

this.restTemplate = new RestTemplate();

下面是我的 gradle 配置:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.0"
    defaultConfig {
        applicationId "com.esp.office"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
        multiDexEnabled true
    }

    lintOptions {
        checkReleaseBuilds false
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            debuggable true
        }
    }

    dexOptions {
        javaMaxHeapSize "4g"
    }

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/LGPL2.1'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:26.0.0'
    compile 'com.android.support:design:26.0.0'
    compile 'com.android.support:cardview-v7:26.0.0'
    compile 'com.android.support:recyclerview-v7:26.0.0'
    compile 'org.springframework.android:spring-android-rest-template:2.0.0.M3'
    compile('org.springframework.android:spring-android-auth:2.0.0.M3') {
        exclude module: 'spring-core'
    }
    compile 'org.springframework.android:spring-android-core:2.0.0.M3'
    compile 'joda-time:joda-time:2.3'
    compile 'com.fasterxml.jackson.core:jackson-databind:2.9.0'
    compile 'com.fasterxml.jackson.core:jackson-annotations:2.9.0'
    compile 'com.fasterxml.jackson.core:jackson-core:2.9.0'

    compile 'com.google.code.gson:gson:2.8.1'
    compile 'com.android.support:multidex:1.0.1'
    compile 'net.sourceforge.jexcelapi:jxl:2.6.12'
    compile 'com.getbase:floatingactionbutton:1.10.1'
    compile 'com.github.ganfra:material-spinner:2.0.0'
    compile 'com.github.wseemann:FFmpegMediaMetadataRetriever:1.0.14'
    compile 'commons-io:commons-io:1.3.2'
    compile 'de.hdodenhof:circleimageview:2.2.0'
    compile 'com.github.bumptech.glide:glide:3.7.0'

    testCompile 'junit:junit:4.12'
}
configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion '26.1.0'
            }
        }
    }

}

构建.gradle:

 buildscript {
 repositories {
    jcenter()
 }
 dependencies {
    classpath 'com.android.tools.build:gradle:2.2.3'
 }
}

allprojects {
  repositories {
    jcenter()
    maven {
        url "https://maven.google.com"
    }
    maven { url "https://jitpack.io" }
    maven { url "https://repo.spring.io/libs-milestone" }

 }
}

task clean(type: Delete) {
 delete rootProject.buildDir
}

出现上述错误的原因是什么,请帮我解决这个问题。

最佳答案

好吧,你的 gradle 中缺少 Maven 存储库。

dependencies {
    compile 'org.springframework.android:spring-android-rest-template:2.0.0.M3'
} repositories {
    maven {
        url 'https://repo.spring.io/libs-milestone'
    }
}

可以看到spring-android-documentation

关于android - java.lang.NoClassDefFoundError : org. springframework.web.client.RestTemplate 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54252444/

相关文章:

eclipse - org.springframework.context.ConfigurableApplicationContext 类型无法解析

spring-mvc - 如何在模块化 Spring Boot 应用程序中定义隔离的 Web 上下文?

java - 将客户端数据放入 Java 对象中。获取 com.fasterxml.jackson.databind.exc.MismatchedInputException

android - getApplicationContext 和 classname.this 的区别

java - AV Pipeline Kit Demo 不工作,有什么问题?

java - Spring Boot 测试中的 MockBean 注解导致 NoUniqueBeanDefinitionException

java - 为生成的 api Rest 客户端配置连接和读取超时

android - 如何使用 ijkplayer 库

java - 更新listView/刷新适配器

java - 如何在我的线程中断后立即中断 RestTemplate 调用?