为什么我的 IntelliJ IDEA 项目没有找到这个导入?

use*_*046 3 java import spring intellij-idea gradle

尝试遵循此处的指南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')
}
Run Code Online (Sandbox Code Playgroud)

And just for context, a screen of my project:

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

For some reason when I hit build, it seems to work fine, but for whatever reason it isn't able to find the org.springframework files such as the imports in this file:

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));
    }
}
Run Code Online (Sandbox Code Playgroud)

Szy*_*iak 5

Did you refreshed Gradle project in your IDEA?

在此处输入图片说明

When you do so you should be able to see all required dependencies in "External Libraries" list.

在此处输入图片说明

EDIT: for some reason your IDEA may not have Gradle configured correctly. You can check in File -> Settings... -> Build, Execution, Deployment -> Build Tools -> Gradle if your project is linked correctly with Gradle:

在此处输入图片说明

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

我希望它有帮助。