Sub topic 7
Tour of the generated project structure
This chapter walks through the files and directories that Spring Initializr produces. It assumes you can read basic Java, have used Maven or Gradle at a user level, and have already generated a project with Initializr in the previous chapter. The examples use Maven, Java, a jar packaging type, group com.example, and artifact demo. The Gradle differences are covered in their own section.
Why the layout matters
Spring Boot relies on conventions more than on configuration. The build tool expects source code in fixed locations. Spring Boot expects the main class in a particular place relative to your other classes. Configuration files go in a known folder, and Boot finds them by name. If you put a file in the wrong place, you usually get no error. The file is silently ignored, or an endpoint returns 404. Knowing the layout helps you avoid these failures.
The top-level view
After you unzip the Initializr download, the project looks like this:
demo/
├── .gitignore
├── .mvn/
│ └── wrapper/
│ └── maven-wrapper.properties
├── HELP.md
├── mvnw
├── mvnw.cmd
├── pom.xml
└── src/
├── main/
│ ├── java/
│ │ └── com/example/demo/
│ │ └── DemoApplication.java
│ └── resources/
│ ├── application.properties
│ ├── static/
│ └── templates/
└── test/
└── java/
└── com/example/demo/
└── DemoApplicationTests.javaThe exact contents vary a little between Initializr versions. For example, static/ and templates/ appear only when a dependency such as the web starter needs them.
Build and wrapper files
The build files sit in the project root.
pom.xmlis the Maven build file. It declares the Spring Boot parent, your dependencies, and the Spring Boot Maven plugin. The next chapter reads it in detail.mvnwandmvnw.cmdare the Maven wrapper scripts for Unix-like systems and Windows. They download and run a specific Maven version, so you do not need Maven installed..mvn/wrapper/maven-wrapper.propertiesrecords which Maven version the wrapper downloads. Commit this folder to version control so every developer and CI job uses the same Maven version.
If you chose Gradle, you get build.gradle (or build.gradle.kts for the Kotlin DSL), settings.gradle, gradlew, gradlew.bat, and a gradle/wrapper/ folder. These play the same roles. Gradle also writes its output to build/ where Maven writes to target/.
Housekeeping files
Two other files in the root are not part of the build.
.gitignorealready excludes build output (target/), IDE folders, and similar files. Check it before adding anything to version control.HELP.mdholds links to reference documentation for the dependencies you selected. It is safe to edit or delete. Nothing reads it.
src/main/java: application code
Maven treats src/main/java as the root of your production source code. Beneath it, the folders mirror your Java package name. With the package com.example.demo, the main class lives at src/main/java/com/example/demo/DemoApplication.java.
This main class is the application's entry point:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}The location of this class matters because of component scanning. @SpringBootApplication makes Spring scan the main class's package and all of its sub-packages for components such as controllers, services, and repositories. Classes outside that package tree are not found.
The package rule in practice
Suppose you add a controller like this one:
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello, Spring Boot!";
}
}It sits in com.example.demo, the same package as the main class, so Spring Boot finds it. If you moved it to com.example.web, the application would still start, but GET /hello would return 404. The controller is outside the scanned tree, so Spring never creates it.
You have two ways to fix this. The simple fix is to keep everything under the main class's package. A common layout is sub-packages such as com.example.demo.web and com.example.demo.service. The alternative is to list extra packages explicitly with @SpringBootApplication(scanBasePackages = ...). Prefer the first option, because it needs no extra configuration.
src/main/resources: configuration and assets
Everything in src/main/resources is copied onto the classpath when the application is built. Spring Boot looks here for several things.
| Path | Purpose |
|---|---|
| `application.properties` | Settings such as the server port. It is empty when generated. You can replace it with `application.yml`. |
| `static/` | Static files served as-is over HTTP, such as HTML, CSS, JavaScript, and images. A file `static/logo.png` is served at `/logo.png`. |
| `templates/` | Server-side templates, used only if you add a template engine such as Thymeleaf. Files here are not served directly. |
The application.properties file is where the next configuration chapters start. A small example:
server.port=8081
spring.application.name=demoBoot reads the file automatically, so you do not register it anywhere. It must be named exactly application.properties (or application.yml) and sit at the root of the classpath, which means directly inside src/main/resources.
Empty directories are not tracked by Git. If static/ or templates/ is empty, it will be missing after a fresh clone. This is harmless, because Boot does not need the folder to exist unless you put files in it.
src/test/java: tests
Test code lives in src/test/java and uses the same package structure as the code it tests. Initializr generates one test:
package com.example.demo;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class DemoApplicationTests {
@Test
void contextLoads() {
}
}The test body is empty on purpose. @SpringBootTest starts the full application context, and if the context fails to start, the test fails. This makes it a cheap smoke test: it catches broken configuration, missing beans, and failed auto-configuration. Keep it, and run it with ./mvnw test.
The test sources and the spring-boot-starter-test dependency (scope test) are not included in the packaged application.
Common mistakes
- Controller in the wrong package. The app starts, but the endpoint returns 404. Move the class under the main class's package.
- Misnamed configuration file. A file called
application.propertyorapp.propertiesis ignored. Boot does not warn you. - Configuration in the wrong folder. Placing
application.propertiesin the project root or insrc/main/javahas no effect. It belongs insrc/main/resources. - Editing generated output. Changes in
target/orbuild/are lost on the next build. - Deleting the wrapper files. Teammates and CI servers then need a matching Maven installation, which is a common cause of "works on my machine" problems.
Exercise
You add a class OrderController annotated with @RestController in the package com.example.orders. The main class is com.example.demo.DemoApplication. The application starts without errors, but requests to the controller's endpoint return 404. What is the cause, and what are two ways to fix it?
Show answer
The controller is outside the package tree that @SpringBootApplication scans (com.example.demo and its sub-packages), so Spring never registers it as a bean. Fix it by moving the class into com.example.demo or a sub-package such as com.example.demo.orders, or by naming the extra package explicitly with @SpringBootApplication(scanBasePackages = {"com.example.demo", "com.example.orders"}). Moving the class is the simpler option.
Exercise
You want the embedded server to use port 9000. In which file and folder do you set this, and what exact line do you add?
Show answer
Edit src/main/resources/application.properties and add server.port=9000. The file must be named exactly application.properties and must be directly inside src/main/resources so that it ends up at the root of the classpath.