To be more specific, I'm referring to Section 4.3 in the "Getting Started" page at docs.spring.io (which is currently making use of Spring 3.0.5. I have OpenJDK 17 and I build using Maven 3.9.1 on Linux Mint 20.3).

The code provided puts the class in the default package (as no other package gets created), and this ends up causing problems because of @SpringBootApplication. Ultimately, the application fails to start after executing mvn spring-boot:run.

The solution is to create a package in MyApplication.java. I would also recommend to move the file from src/main/java/MyApplication.java to src/main/java/com/example/myproject/MyApplication.java to be more consistent with the tip provided in "Developing with Spring Boot" documentation's "Structuring Your Code" section.

The changed code would finally look like this:

package com.example.myproject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class MyApplication {

    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

}

Comment From: snicoll

Superseded by #34838

Comment From: wilkinsona

Duplicate of #34513.