Search code examples
javaspringspring-bootgradle

error: cannot find symbol @RestController in Spring Boot on build


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.


Solution

  • 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.

    enter image description here

    my build.gradle file

    enter image description here