即使依赖项存在导入错误

Import error even though dependencies exist

当我将 import org.springframework.data.annotation.Id; 添加到我的 Java 文件时,出现此错误:

The import org.springframework.data cannot be resolved

我的直接想法是我缺少依赖项,但我认为情况并非如此。

这是我的 build.gradle.

的依赖项
dependencies {
    compile('org.springframework.boot:spring-boot-starter-webflux')
    compile('org.synchronoss.cloud:nio-multipart-parser')
    compile('org.springframework.boot:spring-boot-starter-thymeleaf')
    compile('org.springframework.boot:spring-boot-starter-data-mongodb-reactive')
    compile('org.projectlombok:lombok')
    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile('io.projectreactor:reactor-test')
}

另外,我应用了这些插件:

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

我是不是遗漏了什么,或者我该如何解决这个问题?

附加信息。

我运行./gradlew clean build --refresh-dependencies --debug。这是日志的摘录:

22:25:30.664 [ERROR] [system.err] /Users/me/git/learning-spring-boot/learning-spring-boot-ch2/src/main/java/com/greglturnquist/learningspringboot/learningspringboot/ImageService.java:40: error: constructor Image in class Image cannot be applied to given types;
22:25:30.664 [ERROR] [system.err]                                       new Image(path.hashCode(),
22:25:30.664 [ERROR] [system.err]                                       ^
22:25:30.664 [ERROR] [system.err]   required: String
22:25:30.665 [ERROR] [system.err]   found: int,String
22:25:30.665 [ERROR] [system.err]   reason: actual and formal argument lists differ in length
22:25:30.698 [ERROR] [system.err] /Users/me/git/learning-spring-boot/learning-spring-boot-ch2/src/main/java/com/greglturnquist/learningspringboot/learningspringboot/ApiController.java:26: error: constructor Image in class Image cannot be applied to given types;
22:25:30.700 [ERROR] [system.err]                       new Image("1", "learning-spring-boot-cover.jpg"),
22:25:30.700 [ERROR] [system.err]                       ^
22:25:30.700 [ERROR] [system.err]   required: String
22:25:30.700 [ERROR] [system.err]   found: String,String
22:25:30.701 [ERROR] [system.err]   reason: actual and formal argument lists differ in length
22:25:30.701 [ERROR] [system.err] /Users/me/git/learning-spring-boot/learning-spring-boot-ch2/src/main/java/com/greglturnquist/learningspringboot/learningspringboot/ApiController.java:27: error: constructor Image in class Image cannot be applied to given types;
22:25:30.701 [ERROR] [system.err]                       new Image("2", "learning-spring-boot-2nd-edition-cover.jpg"),
22:25:30.702 [ERROR] [system.err]                       ^
22:25:30.702 [ERROR] [system.err]   required: String
22:25:30.702 [ERROR] [system.err]   found: String,String
22:25:30.702 [ERROR] [system.err]   reason: actual and formal argument lists differ in length
22:25:30.705 [ERROR] [system.err] /Users/me/git/learning-spring-boot/learning-spring-boot-ch2/src/main/java/com/greglturnquist/learningspringboot/learningspringboot/ApiController.java:28: error: constructor Image in class Image cannot be applied to given types;
22:25:30.706 [ERROR] [system.err]                       new Image("3", "bazinga.png")
22:25:30.706 [ERROR] [system.err]                       ^
22:25:30.706 [ERROR] [system.err]   required: String
22:25:30.706 [ERROR] [system.err]   found: String,String
22:25:30.707 [ERROR] [system.err]   reason: actual and formal argument lists differ in length
22:25:30.713 [ERROR] [system.err] 4 errors
22:25:30.716 [DEBUG] [org.gradle.internal.operations.DefaultBuildOperationExecutor] Completing Build operation 'Execute compile for :compileJava'
22:25:30.726 [DEBUG] [org.gradle.internal.operations.DefaultBuildOperationExecutor] Build operation 'Execute compile for :compileJava' completed
22:25:30.733 [DEBUG] [org.gradle.api.internal.changedetection.state.CacheBackedTaskHistoryRepository] Fingerprinting property destinationDir (Output) for task ':compileJava'
22:25:30.737 [LIFECYCLE] [org.gradle.cache.internal.btree.BTreePersistentIndexedCache] 
22:25:30.737 [DEBUG] [org.gradle.cache.internal.btree.BTreePersistentIndexedCache] Opening cache fileHashes.bin (/Users/me/git/learning-spring-boot/learning-spring-boot-ch2/.gradle/4.10.2/fileHashes/fileHashes.bin)
22:25:30.702 [LIFECYCLE] [class org.gradle.internal.buildevents.TaskExecutionLogger] 
22:25:30.702 [LIFECYCLE] [class org.gradle.internal.buildevents.TaskExecutionLogger] > Task :compileJava FAILED
22:25:30.738 [DEBUG] [org.gradle.api.internal.tasks.execution.ResolveTaskArtifactStateTaskExecuter] Removed task artifact state for {} from context.
22:25:30.739 [DEBUG] [org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter] Finished executing task ':compileJava'
22:25:30.740 [DEBUG] [org.gradle.internal.operations.DefaultBuildOperationExecutor] Completing Build operation 'Task :compileJava'
22:25:30.740 [DEBUG] [org.gradle.internal.operations.DefaultBuildOperationExecutor] Build operation 'Task :compileJava' completed
22:25:30.741 [INFO] [org.gradle.execution.taskgraph.DefaultTaskPlanExecutor] :compileJava (Thread[Task worker for ':',5,main]) completed. Took 59.344 secs.
22:25:30.741 [DEBUG] [org.gradle.internal.resources.AbstractTrackedResourceLock] Task worker for ':': released lock on :
22:25:30.742 [DEBUG] [org.gradle.internal.work.DefaultWorkerLeaseService] Worker lease root.1.13 completed (1 worker(s) in use)
22:25:30.743 [DEBUG] [org.gradle.internal.resources.AbstractTrackedResourceLock] Task worker for ':': released lock on root.1.13
22:25:30.743 [DEBUG] [org.gradle.execution.taskgraph.DefaultTaskPlanExecutor] Task worker [Thread[Daemon worker Thread 7,5,main]] finished, busy: 0.0 secs, idle: 59.358 secs
22:25:30.744 [DEBUG] [org.gradle.execution.taskgraph.DefaultTaskPlanExecutor] Task worker [Thread[Task worker for ':',5,main]] finished, busy: 59.353 secs, idle: 0.006 secs
22:25:30.743 [DEBUG] [org.gradle.execution.taskgraph.DefaultTaskPlanExecutor] Task worker [Thread[Task worker for ':' Thread 3,5,main]] finished, busy: 0.0 secs, idle: 59.358 secs
22:25:30.743 [DEBUG] [org.gradle.execution.taskgraph.DefaultTaskPlanExecutor] Task worker [Thread[Task worker for ':' Thread 2,5,main]] finished, busy: 0.0 secs, idle: 59.36 secs
22:25:30.748 [DEBUG] [org.gradle.execution.taskgraph.DefaultTaskExecutionGraph] Timing: Executing the DAG took 59.365 secs
22:25:30.748 [DEBUG] [org.gradle.internal.operations.DefaultBuildOperationExecutor] Completing Build operation 'Run tasks'
22:25:30.748 [DEBUG] [org.gradle.internal.operations.DefaultBuildOperationExecutor] Build operation 'Run tasks' completed
22:25:30.749 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 
22:25:30.750 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] FAILURE: Build failed with an exception.
22:25:30.750 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 
22:25:30.750 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] * What went wrong:
22:25:30.750 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] Execution failed for task ':compileJava'.
22:25:30.750 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] > Compilation failed; see the compiler error output for details.
22:25:30.750 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 
22:25:30.750 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] * Try:
22:25:30.751 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] Run with --stacktrace option to get the stack trace.  Run with --scan to get full insights.
22:25:30.751 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] 
22:25:30.751 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] * Get more help at https://help.gradle.org
22:25:30.751 [WARN] [org.gradle.internal.featurelifecycle.LoggingDeprecatedFeatureHandler] 
Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/4.10.2/userguide/command_line_interface.html#sec:command_line_warnings
22:25:30.751 [ERROR] [org.gradle.internal.buildevents.BuildResultLogger] 
22:25:30.751 [ERROR] [org.gradle.internal.buildevents.BuildResultLogger] BUILD FAILED in 1m 47s

我检查了 ./gradlew dependencies org.springframework.boot:spring-boot-starter-data-mongodb-reactive -> 2.2.0.BUILD-SNAPSHOT 包含 org.springframework.data:spring-data-mongodb:2.1.4.BUILD-SNAPSHOT 其中包含 org.springframework:spring-context:5.1.3.RELEASE -> 5.1.4.BUILD-SNAPSHOT。有没有可能org.springframework:spring-context:5.1.3.RELEASE -> 5.1.4.BUILD-SNAPSHOT不是兼容版本?

您确定 data 是导入包的一部分吗?在我看来,您需要将 org.springframework.data 添加到 gradle 文件中。您可以看到 here data 是不同的包。

这个classorg.springframework.data.annotation.Id belongs to the Spring module org.springframework.data:spring-data-commons。所以如果你想从这个模块导入 classes 你应该给它添加一个依赖:

dependencies{
     compile("org.springframework.data:spring-data-commons")
     // others ...
}

注意 你也应该能够导入这个 class 而不显式依赖于 spring-data-commons 模块,因为这个模块已经是传递的一部分项目的依赖项(来自 spring-boot-starter-data-mongodb-reactive)。这意味着使用您当前的 build.gradle 设置,您应该能够正确导入此 class。

./gradlew dependencies 

+--- org.springframework.boot:spring-boot-starter-data-mongodb-reactive -> 2.0.0.RELEASE
|    +--- org.springframework.boot:spring-boot-starter:2.0.0.RELEASE (*)
|    +--- org.springframework.data:spring-data-mongodb:2.0.5.RELEASE
|    |    +--- org.springframework:spring-tx:5.0.4.RELEASE
|    |    |    +--- org.springframework:spring-beans:5.0.4.RELEASE (*)
|    |    |    \--- org.springframework:spring-core:5.0.4.RELEASE (*)
|    |    +--- org.springframework:spring-context:5.0.4.RELEASE (*)
|    |    +--- org.springframework:spring-beans:5.0.4.RELEASE (*)
|    |    +--- org.springframework:spring-core:5.0.4.RELEASE (*)
|    |    +--- org.springframework:spring-expression:5.0.4.RELEASE (*)
|    |    +--- org.springframework.data:spring-data-commons:2.0.5.RELEASE (*)
|    |    \--- org.slf4j:slf4j-api:1.7.25

所以也许您的项目中存在同步问题,请尝试 clean/close/reopen 它。