【问题标题】:Why is my IntelliJ IDEA project not finding this import?为什么我的 IntelliJ IDEA 项目找不到此导入?
【发布时间】:2017-12-20 15:40:05
【问题描述】:

试着按照这里的指南https://spring.io/guides/gs/rest-service/#scratch

这是我的 Gradle 文件

version '1.0'

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.3.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'

jar {
    baseName = 'gs-rest-service'
    version =  '0.1.0'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
    compile("org.springframework.boot:spring-boot-starter-web")
    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile('com.jayway.jsonpath:json-path')
}

只是为了上下文,我的项目的屏幕:

https://i.stack.imgur.com/m8aqE.png

由于某种原因,当我点击构建时,它似乎工作正常,但无论出于何种原因,它都无法找到 org.springframework 文件,例如此文件中的导入:

package hello;

import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @RequestMapping("/greeting")
    public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
        return new Greeting(counter.incrementAndGet(),
                String.format(template, name));
    }
}

【问题讨论】:

  • 打开 Gradle 选项卡(应该在窗口的右侧)并刷新您的依赖项。
  • 我没有看到 Gradle 选项卡
  • 没关系,查看 -> 工具窗口 -> Gradle 为我提供了窗口,点击刷新。似乎现在可以工作了!谢谢!每次更改我的gradle时我都必须这样做吗?在 Android Studio 中我习惯于 Build 处理所有事情,我猜这里不一样?

标签: java spring intellij-idea gradle import


【解决方案1】:

您是否在 IDEA 中刷新了 Gradle 项目?

当您这样做时,您应该能够在“外部库”列表中看到所有必需的依赖项。

编辑:由于某种原因,您的 IDEA 可能没有正确配置 Gradle。您可以检查 文件 -> 设置... -> 构建、执行、部署 -> 构建工具 -> Gradle 如果您的项目与 Gradle 正确链接:

如果您想让 Gradle 自动为您导入所有依赖项,您可以标记“使用自动导入”。唯一的问题是它可能会消耗大量资源并使您的 IDE 运行缓慢。这听起来可能不是一个方便的解决方案,但我认为在需要时手动刷新项目效果更好。

希望对你有帮助。

【讨论】:

  • 是的,看起来还可以
  • 在更改依赖项时使用 gradle 工具栏刷新窗口是否正常,还是大多数人使用它类似于我使用 Android Studio 的方式? (点击构建,其他一切似乎都可以工作/导入/等)
  • 如果您想让 Gradle 自动为您导入所有依赖项,您可以标记“使用自动导入”。唯一的问题是它可能会消耗大量资源并使您的 IDE 运行缓慢。这听起来可能不是一个方便的解决方案,但我认为在需要时手动刷新项目效果更好。
  • 当您说 IDE 运行缓慢时,您是指仅在构建过程中,还是永久等?
  • 几年前我曾标记过一次 - build.gradle(或相关)文件中的任何小更改都会迫使 Gradle 刷新依赖项,这使我的 IDEA 运行非常缓慢。从那以后我没有测试过自动导入,我只是习惯了每次在 build.gradle 文件中添加/修改依赖项时手动导入:)
【解决方案2】:

尝试添加maven依赖spring-web

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>

https://mvnrepository.com/artifact/org.springframework/spring-web/5.1.5.RELEASE

【讨论】:

    猜你喜欢
    • 2014-12-02
    • 2021-11-28
    • 2011-10-12
    • 1970-01-01
    • 2011-02-07
    • 2019-01-23
    • 2014-09-16
    • 1970-01-01
    相关资源
    最近更新 更多