最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - error: cannot find symbol @RestController in Spring Boot on build - Stack Overflow

programmeradmin2浏览0评论

I am trying to deploy a minimal example of an API using Spring Boot, automating the building task using Gradle as suggested by the documentation.

package webserver.blog;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@RestController
@SpringBootApplication
public class BlogApplication {
    @RequestMapping("/")
    String home() {
        return "Hello, World!";
    }
    public static void main(String[] args) {
        SpringApplication.run(BlogApplication.class, args);
    }
}

However, I receive an error by Java - I cited Gradle just for completeness but I would assume the error is not caused from the automation side given this error.

org.gradle.api.internal.taskspile.CompilationFailedException: Compilation failed; see the compiler output below.
/home/flak-zoso/IdeaProjects/ics0014/SpringBlog/blog/src/main/java/webserver/blog/BlogApplication.java:7: error: cannot find symbol
@RestController
 ^
  symbol: class RestController
/home/flak-zoso/IdeaProjects/ics0014/SpringBlog/blog/src/main/java/webserver/blog/BlogApplication.java:11: error: cannot find symbol
    @RequestMapping("/")
     ^
  symbol:   class RequestMapping
  location: class BlogApplication
2 errors

    at org.gradle.api.internal.taskspile.JdkJavaCompiler.execute(JdkJavaCompiler.java:84)

The content of my build.gradle file is the following.

plugins {
    id 'java'
    id 'application'
    id 'org.springframework.boot' version '3.4.2'
    id 'io.spring.dependency-management' version '1.1.7'
}

group = 'webserver'
version = '0.0.1-SNAPSHOT'

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
}

repositories {
    mavenCentral()
}

application {
    mainClass.set('main.java.webserver.BlogApplication')
}
sourceSets.main.java.srcDirs = ['blog/src']

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework:spring-web'
    implementation 'org.liquibase:liquibase-core'
    implementation 'org.junit.jupiter:junit-jupiter-api:5.5.1'
    testImplementation 'org.springframework.boot.test.context:spring-boot-starter-test'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

tasks.named('test') {
    useJUnitPlatform()
}

So I seem to miss this @RequestMapping class as well as @RestController, but in my dependencies and in my imports I included all what examples found online as well as the documentation do, apparently.

In the doubt, I even tried adding a more comprehensive import, but it didn't change the error.

import org.springframework.boot.*;

Also, as suggested by @Syed in the comments, I modified the build configuration. With no success.

application {
    mainClass.set('webserver.blog.BlogApplication')
}
sourceSets.main.java.srcDirs = ['blog/src/main/java']

What package, dependency, or import am I missing? Thanks.

I am trying to deploy a minimal example of an API using Spring Boot, automating the building task using Gradle as suggested by the documentation.

package webserver.blog;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@RestController
@SpringBootApplication
public class BlogApplication {
    @RequestMapping("/")
    String home() {
        return "Hello, World!";
    }
    public static void main(String[] args) {
        SpringApplication.run(BlogApplication.class, args);
    }
}

However, I receive an error by Java - I cited Gradle just for completeness but I would assume the error is not caused from the automation side given this error.

org.gradle.api.internal.tasks.compile.CompilationFailedException: Compilation failed; see the compiler output below.
/home/flak-zoso/IdeaProjects/ics0014/SpringBlog/blog/src/main/java/webserver/blog/BlogApplication.java:7: error: cannot find symbol
@RestController
 ^
  symbol: class RestController
/home/flak-zoso/IdeaProjects/ics0014/SpringBlog/blog/src/main/java/webserver/blog/BlogApplication.java:11: error: cannot find symbol
    @RequestMapping("/")
     ^
  symbol:   class RequestMapping
  location: class BlogApplication
2 errors

    at org.gradle.api.internal.tasks.compile.JdkJavaCompiler.execute(JdkJavaCompiler.java:84)

The content of my build.gradle file is the following.

plugins {
    id 'java'
    id 'application'
    id 'org.springframework.boot' version '3.4.2'
    id 'io.spring.dependency-management' version '1.1.7'
}

group = 'webserver'
version = '0.0.1-SNAPSHOT'

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
}

repositories {
    mavenCentral()
}

application {
    mainClass.set('main.java.webserver.BlogApplication')
}
sourceSets.main.java.srcDirs = ['blog/src']

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework:spring-web'
    implementation 'org.liquibase:liquibase-core'
    implementation 'org.junit.jupiter:junit-jupiter-api:5.5.1'
    testImplementation 'org.springframework.boot.test.context:spring-boot-starter-test'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

tasks.named('test') {
    useJUnitPlatform()
}

So I seem to miss this @RequestMapping class as well as @RestController, but in my dependencies and in my imports I included all what examples found online as well as the documentation do, apparently.

In the doubt, I even tried adding a more comprehensive import, but it didn't change the error.

import org.springframework.boot.*;

Also, as suggested by @Syed in the comments, I modified the build configuration. With no success.

application {
    mainClass.set('webserver.blog.BlogApplication')
}
sourceSets.main.java.srcDirs = ['blog/src/main/java']

What package, dependency, or import am I missing? Thanks.

Share Improve this question edited yesterday FLAK-ZOSO asked yesterday FLAK-ZOSOFLAK-ZOSO 4,0925 gold badges10 silver badges33 bronze badges 6
  • Your mainClass is not set correctly. Update it to match your package structure: mainClass.set('webserver.blog.BlogApplication') Also, ensure your source sets are configured properly: sourceSets { main { java { srcDirs = ['src/main/java'] } } } – Syed Commented yesterday
  • I doubt that the error comes from there, since previously it did but I changed it in order to let it find the main class. I will try with your suggestion, but I don't believe the error comes from there. EDIT: no change in the errors after these changes. – FLAK-ZOSO Commented yesterday
  • 2 You need to import these two annotations: import org.springframework.web.bind.annotation.RequestMapping; and import org.springframework.web.bind.annotation.RestController;. Use springBoot {mainClass.set('webserver.blog.BlogApplication')} and remove the application plugin. BR – Roar S. Commented yesterday
  • Thank you @RoarS. for your help. Is there a standard way to find in which "sublibrary" a certain class is contained? I understand that it is probably not meant to always be a feature, but if there is an handy way to do so (such as performing "man" searches in Linux) it might save me some time and some dumb/downvoted questions such as this one. – FLAK-ZOSO Commented yesterday
  • 1 Using a "smart" IDEA, like IntelliJ, is a must, it will suggest the missing imports. Use start.spring.io for creating new applications. – Roar S. Commented yesterday
 |  Show 1 more comment

1 Answer 1

Reset to default 2

You have to import @RestController and @RequestMapping from:

org.springframework.web.bind.annotation.RequestMapping;
org.springframework.web.bind.annotation.RestController;

Additionally, you need to set the main class properly:

mainClass.set('webserver.blog.BlogApplication');

As you can see below, my Spring app has been started successfully without any errors.

my build.gradle file

发布评论

评论列表(0)

  1. 暂无评论