Skip to lesson

Project layout and the main class

Where does a new ShopLite controller go, and what do you check when the app will not start? A new project has only a handful of files, and each has one job, so knowing them answers both questions.

pom.xml: the parent, java.version and the Boot plugin

spring-boot-starter-parent is the pom that your pom.xml inherits from: it fixes the versions of hundreds of libraries and sets sensible plugin defaults, which is why starters need no <version>.

Example 1 is the generated pom.xml. Three parts are specific to Spring Boot: the parent, java.version and the Boot plugin.

Example 1 · pom.xml

Runs in: the shoplite-api Spring Boot project

XML
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>4.1.1</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>    <groupId>com.shoplite</groupId>    <artifactId>shoplite-api</artifactId>    <version>0.0.1-SNAPSHOT</version>    <name>shoplite-api</name>    <description>ShopLite REST API</description>    <url/>    <licenses>        <license/>    </licenses>    <developers>        <developer/>    </developers>    <scm>        <connection/>        <developerConnection/>        <tag/>        <url/>    </scm>    <properties>        <java.version>17</java.version>    </properties>    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-webmvc</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-webmvc-test</artifactId>            <scope>test</scope>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build></project>
Output of mvnw -q help:evaluate for maven.compiler.release, then project.parent.version
17
4.1.1

What this code does

  • The parent brings managed versions, so the two starters have no <version>. spring-boot-starter-webmvc-test has <scope>test</scope>: it is used only for tests and stays out of the jar.
  • The output shows two values Maven worked out from this file: the compiler release, 17, comes from java.version; the parent version is 4.1.1. They were printed with mvnw -q help:evaluate -Dexpression=<name> -DforceStdout.

pom.xml is the Maven build file: it names the project, its Java version, its libraries and its build plugins. The java.version property (17) is turned by the parent into the compiler's release setting. The spring-boot-maven-plugin adds the spring-boot:run goal you used and, during package, turns the plain jar into a runnable one.

The empty <license/>, <developers/> and <scm> tags stop the parent's own values from being inherited. They are harmless; leave them or fill them in.

src/main/java, src/main/resources and src/test

application.properties is Spring Boot's main settings file, in src/main/resources: Initializr writes spring.application.name=shoplite-api into it, and settings such as server.port=8081 go here too.

Example 2 lists every file and folder of a new project. Code goes in src/main/java, settings in src/main/resources, tests in src/test/java.

Example 2 · The project's files and folders

Runs in: the shoplite-api Spring Boot project

Terminal
tree /f /a
Sample output (Windows)
Folder PATH listing for volume Windows
Volume serial number is A191-D090
C:.
|   .gitattributes
|   .gitignore
|   HELP.md
|   mvnw
|   mvnw.cmd
|   pom.xml
|
+---.mvn
|   \---wrapper
|           maven-wrapper.properties
|
\---src
    +---main
    |   +---java
    |   |   \---com
    |   |       \---shoplite
    |   |               ShopliteApiApplication.java
    |   |
    |   \---resources
    |       |   application.properties
    |       |
    |       +---static
    |       \---templates
    \---test
        \---java
            \---com
                \---shoplite
                        ShopliteApiApplicationTests.java

What this code does

  • tree /f /a on Windows prints every folder and file. On macOS use find . -not -path './.git*' or look in Finder; the files are the same.
  • Before the first build there is no target folder. static and templates are empty folders, which Git will not store until they hold a file.

Maven expects code in fixed folders, so no build setting is needed. src/main/java holds your Java code in package folders, src/main/resources holds the non-Java files that go into the jar, and src/test/java holds tests, which run during the build but never go into the jar. The name shoplite-api is the one you saw in brackets in every log line. static (images, CSS, plain HTML) and templates (server-rendered HTML pages) are for websites; a REST API like ShopLite leaves them empty, and deleting them is fine.

The other files:

  • mvnw, mvnw.cmd, .mvn/wrapper: the Maven wrapper. Commit them.
  • .gitignore: already ignores target/ and IDE folders.
  • HELP.md: links to guides. Safe to delete.
  • target/: created by builds. Never edit or commit it.

The main class: @SpringBootApplication and SpringApplication.run

@SpringBootApplication marks the main class as the starting point of a Spring Boot app: it makes the class a configuration class and switches on auto-configuration and component scanning.

Example 3 is the main class exactly as Initializr writes it. Its only line of code is SpringApplication.run(ShopliteApiApplication.class, args).

Example 3 · src/main/java/com/shoplite/ShopliteApiApplication.java

Runs in: the shoplite-api Spring Boot project

ShopliteApiApplication.javaJava
package com.shoplite;
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplicationpublic class ShopliteApiApplication {
    public static void main(String[] args) {        SpringApplication.run(ShopliteApiApplication.class, args);    }
}
Sample output
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

 :: Spring Boot ::                (v4.1.1)

2026-10-16T08:07:25.149+05:30  INFO 22388 --- [shoplite-api] [           main] com.shoplite.ShopliteApiApplication      : Starting ShopliteApiApplication v0.0.1-SNAPSHOT using Java 19.0.2 with PID 22388 (C:\Users\asha\shoplite-api\target\shoplite-api-0.0.1-SNAPSHOT.jar started by asha in C:\Users\asha\shoplite-api)
2026-10-16T08:07:25.158+05:30  INFO 22388 --- [shoplite-api] [           main] com.shoplite.ShopliteApiApplication      : No active profile set, falling back to 1 default profile: "default"
2026-10-16T08:07:27.659+05:30  INFO 22388 --- [shoplite-api] [           main] o.s.boot.tomcat.TomcatWebServer          : Tomcat initialized with port 8080 (http)
2026-10-16T08:07:27.698+05:30  INFO 22388 --- [shoplite-api] [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2026-10-16T08:07:27.698+05:30  INFO 22388 --- [shoplite-api] [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/11.0.24]
2026-10-16T08:07:27.780+05:30  INFO 22388 --- [shoplite-api] [           main] b.w.c.s.WebApplicationContextInitializer : Root WebApplicationContext: initialization completed in 2474 ms
2026-10-16T08:07:28.862+05:30  INFO 22388 --- [shoplite-api] [           main] o.s.boot.tomcat.TomcatWebServer          : Tomcat started on port 8080 (http) with context path '/'
2026-10-16T08:07:28.897+05:30  INFO 22388 --- [shoplite-api] [           main] com.shoplite.ShopliteApiApplication      : Started ShopliteApiApplication in 5.129 seconds (process running for 6.875)

What this code does

  • Exactly as Initializr wrote it. SpringApplication.run produced every line of this log: the container, Tomcat on port 8080, and Started ShopliteApiApplication.
  • v0.0.1-SNAPSHOT appears after the class name because this run was from the packaged jar, which knows its version. Times and the process id differ on your computer.

The main class is the class with main that starts everything; Initializr names it after the project, ShopliteApiApplication. SpringApplication.run creates the Spring container, finds your classes, runs auto-configuration, starts Tomcat and returns once the app is ready. The args from the command line go in too, which is how --server.port=8081 reaches Spring.

The How Spring Boot Works module takes @SpringBootApplication apart piece by piece. For now, remember what it switches on: the container finds your classes by scanning packages, which the next section explains.

Keep the main class nearly empty. Startup work, settings and objects belong in their own classes, which later modules add.

Why the main class sits in the root package com.shoplite

Component scanning means Spring looks through the main class's package and every sub-package for classes marked as Spring components, such as controllers.

Example 4 puts a controller in com.hello, outside the root package, and /hello answers 404. Example 5 moves it below com.shoplite.

Example 4 · src/main/java/com/hello/HelloController.java (outside the root package)

Runs in: the shoplite-api Spring Boot project

HelloController.javaJava
package com.hello;
import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;
@RestControllerpublic class HelloController {
    @GetMapping("/hello")    public String hello() {        return "Hello from ShopLite";    }}
Sample output (curl -i http://localhost:8080/hello)
HTTP/1.1 404 
Content-Type: application/json
Transfer-Encoding: chunked
Date: Fri, 16 Oct 2026 04:44:42 GMT

{"timestamp":"2026-10-16T04:44:41.967Z","status":404,"error":"Not Found","path":"/hello"}

What this code does

  • @RestController and @GetMapping("/hello") say "answer GET /hello with this method"; the REST Controllers module explains them. The class compiles and the app starts without any error.
  • Yet /hello is 404: com.hello is not under com.shoplite, so component scanning never found the controller.

Example 5 · src/main/java/com/shoplite/hello/HelloController.java (fixed)

Runs in: the shoplite-api Spring Boot project

HelloController.javaJava
package com.shoplite.hello;
import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;
@RestControllerpublic class HelloController {
    @GetMapping("/hello")    public String hello() {        return "Hello from ShopLite";    }}
Sample output (curl -i http://localhost:8080/hello)
HTTP/1.1 200 
Content-Type: text/plain;charset=UTF-8
Content-Length: 19
Date: Fri, 16 Oct 2026 04:45:26 GMT

Hello from ShopLite

What this code does

  • The only change is the package line and the folder: com/shoplite/hello. Now the class is below the main class's package and is scanned.
  • The same request now gets 200 and the text body. This demo controller is not part of ShopLite; delete it after trying it.

Scanning starts at the main class's package and goes down into every sub-package. The main class is in com.shoplite, so com.shoplite.product, com.shoplite.order and every other ShopLite package are scanned, while a class in com.hello is outside that tree and is never found. The symptom is quiet: the app starts normally and the endpoint answers 404 as if it did not exist, with no error pointing at the problem.

The rule: keep the main class in the root package com.shoplite, and put every other class in a package below it. The feature packages of the ShopLite project need exactly that.

Find the bug

Find the package that breaks scanning

Tap the line that stops Spring from finding the class.

GET /hello answers 404 although the app starts. Which line is wrong?

Round 1 of 3
A package is not "below" just because its name starts the same way: com.shoplitex is not inside com.shoplite. Only com.shoplite. followed by more parts is.

Packaging a runnable jar: mvnw package and java -jar

An executable (fat) jar is one jar that holds your classes, every library jar and embedded Tomcat, so java -jar starts the app with no Maven and no IDE.

Example 6 builds the jar with mvnw clean package and starts it with java -jar.

Example 6 · Build the jar, then run it

Runs in: the shoplite-api Spring Boot project

Terminal
mvnw clean packagejava -jar target/shoplite-api-0.0.1-SNAPSHOT.jar
Sample output (the end of mvnw clean package)
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.shoplite.ShopliteApiApplicationTests
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 18.14 s -- in com.shoplite.ShopliteApiApplicationTests
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] 
[INFO] --- jar:3.5.1:jar (default-jar) @ shoplite-api ---
[INFO] Building jar: C:\Users\asha\shoplite-api\target\shoplite-api-0.0.1-SNAPSHOT.jar
[INFO] 
[INFO] --- spring-boot:4.1.1:repackage (repackage) @ shoplite-api ---
[INFO] Replacing main artifact C:\Users\asha\shoplite-api\target\shoplite-api-0.0.1-SNAPSHOT.jar with repackaged archive, adding nested dependencies in BOOT-INF/.
[INFO] The original artifact has been renamed to C:\Users\asha\shoplite-api\target\shoplite-api-0.0.1-SNAPSHOT.jar.original
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS

What this code does

  • clean deletes the old target folder first. The one generated test, contextLoads, starts the whole app once to check it can start; Tests run: 1, Failures: 0 means it passed.
  • repackage turns the plain jar into the runnable one and keeps the plain one as .jar.original. java -jar then prints the same startup log as Example 3; stop it with Ctrl+C.

mvnw package compiles the code, runs the tests and builds a jar in target/. The Boot plugin then repackages it: your classes go in BOOT-INF/classes and every library jar in BOOT-INF/lib, which makes about 19 MB for this almost empty project. java -jar target/shoplite-api-0.0.1-SNAPSHOT.jar then starts the app on any computer with Java 17 or newer, and this one file is what you deploy to a server.

What can go wrong: running the small .jar.original file, or a jar built without the Boot plugin, gives no main manifest attribute. A failing test also stops package with BUILD FAILURE; fix the test rather than skipping tests.

Next step

Check your understanding

12 quick questions