Sub topic 7
Tour of the Generated Project Structure
This chapter assumes you can read basic Java and Maven, and that you have already generated a project with Spring Initializr. Here you will learn where each generated file lives and what it does, so you know where to put your own code.
The layout at a glance
An Initializr project for Maven with Java looks like this:
demo/
├── .mvn/wrapper/
├── mvnw
├── mvnw.cmd
├── pom.xml
├── HELP.md
├── .gitignore
└── src/
├── main/
│ ├── java/com/example/demo/DemoApplication.java
│ └── resources/
│ ├── application.properties
│ ├── static/
│ └── templates/
└── test/
└── java/com/example/demo/DemoApplicationTests.javaThe structure follows the standard Maven directory layout. Spring Boot adds almost no custom structure of its own. Most of what you see comes from Maven conventions, and Boot only adds a few resource folders that it knows how to use.
Build files and the wrapper
pom.xml is the Maven build file. It declares the Spring Boot parent, your dependencies, and the Spring Boot Maven plugin. A later chapter reads it in detail.
mvnw and mvnw.cmd are the Maven wrapper scripts for Unix and Windows. The .mvn/wrapper folder holds their settings. The wrapper downloads the Maven version the project expects, so you do not need Maven installed and every developer builds with the same version. Commit these files to version control.
If you chose Gradle, you get build.gradle, settings.gradle, and gradlew instead.
Application code: src/main/java
Your Java sources go under src/main/java. Initializr creates a package from the group and artifact names you entered, such as com.example.demo, and places the main class there.
The package location matters. @SpringBootApplication scans the main class's package and all packages below it for components. A class in com.example.other would not be found. Keep your controllers, services, and repositories in sub-packages of the main class's package.
package com.example.demo.web;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PingController {
@GetMapping("/ping")
public String ping() {
return "pong";
}
}This controller sits in com.example.demo.web, a sub-package of com.example.demo, so component scanning finds it.
Resources: src/main/resources
Files here are copied onto the classpath when you build.
| Path | Purpose |
|---|---|
| `application.properties` | Configuration. Empty at first, or holds only the application name. You can replace it with `application.yml`. |
| `static/` | Static files such as HTML, CSS, and JavaScript, served from the web root. |
| `templates/` | Server-side templates, used only if you add a template engine starter such as Thymeleaf. |
The static and templates folders are empty in a new project. Git does not track empty folders, so they may not appear after a fresh clone. Create them when you need them.
Tests: src/test/java
The test starter provides JUnit and the Spring testing tools. Initializr generates one test class, which mirrors the main class's package:
package com.example.demo;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class DemoApplicationTests {
@Test
void contextLoads() {
}
}The empty test passes only if the whole application context starts. It is a cheap check that your configuration and bean wiring are valid.
Other files
.gitignoreexcludes build output and IDE files, such astarget/.HELP.mdholds links to reference documentation for the dependencies you chose. You can delete it.target/appears after your first build and holds compiled classes and the packaged JAR. Never commit it. Gradle usesbuild/instead.
Exercise
You add a class annotated with @RestController in the package com.example.controllers. Your main class is in com.example.demo. Why is the endpoint not available, and how do you fix it?
Show answer
Component scanning from @SpringBootApplication covers only com.example.demo and its sub-packages, so com.example.controllers is never scanned. Move the class into a sub-package such as com.example.demo.controllers, or move the main class to a common parent package such as com.example.