Spring Boot Tutorial 0/110 lessons ~6 min read Lesson 7

    First Application

    A first Spring Boot 3 application on Java 21 is a walking skeleton.

    Course progress0%
    Focus
    12 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    A first Spring Boot 3 application on Java 21 is a walking skeleton. SpringApplication.run builds the IoC container, auto-configures from the classpath, and starts embedded Tomcat on port 8080. You ship a process, not a WAR for an external server.

    @SpringBootApplication on OrdersApplication is @SpringBootConfiguration (a specialized @Configuration), @EnableAutoConfiguration, and @ComponentScan of that package and its children. spring-boot-starter-web brings Jackson, DispatcherServlet, and Tomcat without a @Bean you wrote.

    The body is a Java 21 record. Jackson emits {id, status, amountCents} so the client can lock the contract before a database exists. Money is minor units, an int, so the skeleton never grows a floating-point price.

    The runnable artifact is a fat JAR. Since Boot 3.2, Main-Class is org.springframework.boot.loader.launch.JarLauncher and Start-Class is your main. Classes live in BOOT-INF/classes, dependencies in BOOT-INF/lib. ./mvnw spring-boot:run is the inner loop; java -jar is what CI and production run.

    Purpose of this lesson

    Build, run, and package the TechLearningPro Orders skeleton on Spring Boot 3.5 and Java 21, and explain a failed boot: port in use, controller outside the scan, or a thin JAR.

    Understanding the topic

    Each startup stage has a log line or a failure you should recognize.

    • Build: parent spring-boot-starter-parent 3.5.x, java.version 21, and the parent's -parameters flag so Jackson can bind records. Commit mvnw.
    • Scan: beans must live in com.techlearningpro.orders or a subpackage. A sibling package is invisible.
    • Contract: record components become JSON keys (amountCents) unless a Jackson naming strategy says otherwise.
    • Artifact: spring-boot-maven-plugin repackages on package. Do not launch *.jar.original.

    Step-by-step explanation

    1. Generate with spring-boot-starter-web, Java 21, group com.techlearningpro.
    2. Place OrdersApplication in com.techlearningpro.orders and keep the controller underneath it.
    3. Run ./mvnw spring-boot:run until Tomcat started on port 8080 (http).
    4. Curl /api/orders/42, then restart the fat JAR with SERVER_PORT=9090.

    Informative example

    Orders walking skeleton. Package-private types share this listing; split public types into their own files in the repo.

    java
    package com.techlearningpro.orders;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.web.bind.annotation.*;
    @SpringBootApplication
    public class OrdersApplication {
    public static void main(String[] args) {
    SpringApplication.run(OrdersApplication.class, args);
    }
    }
    @RestController
    @RequestMapping("/api/orders")
    class OrderController {
    @GetMapping("/{id}")
    OrderView getOne(@PathVariable long id) {
    return new OrderView(id, "PENDING", 4999);
    }
    }
    record OrderView(long id, String status, int amountCents) {}

    Output

    {"id":42,"status":"PENDING","amountCents":4999}

    Real-world use

    TechLearningPro Orders is the checkout API this course grows: quote, reserve, persist, then secure. On day one the mobile squad needs GET /api/orders/{id} and amountCents. Later lessons replace the hardcoded record with OrderService and JPA without changing that path or the minor-unit amount.

    Security implications

    The sample route is public fixture data. Shipping it unchanged is an open order lookup.

    • Boot 3 defaults server.error.include-stacktrace, include-message, and include-binding-errors to never. Pin them so a profile cannot flip them to always.
    • Remote devtools (spring.devtools.remote.secret) is remote code execution. Leave it off. Do not expose /actuator/env.
    • When getOne reads a real row, authorize it. A public id is an insecure direct object reference. Run the container as a non-root user.

    Best practices

    • Keep OrdersApplication in the root package and application code beneath it.
    • Return a record per endpoint. Store money as minor units.
    • Mark spring-boot-devtools optional. Repackage leaves it out of the fat JAR, and java -jar disables restart.
    • Log order ids. Leave customer payloads off the info line.

    Common mistakes

    • A main class in the default package. Boot refuses to scan the entire classpath.
    • A controller outside com.techlearningpro.orders. The process is healthy and every route is 404.
    • Boot 2 snippets that import javax.*. Boot 3 is jakarta.* for servlet, validation, and JPA.
    • Hardcoding 8080 in a TomcatServletWebServerFactory. SERVER_PORT and --server.port already exist, and the argument wins.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    3 questions
    1BeginnerQuestionWhat does @SpringBootApplication enable?+

    Answer

    @SpringBootConfiguration, @EnableAutoConfiguration, and @ComponentScan from that package down. starter-web contributes Tomcat, MVC, and Jackson because they are on the classpath.

    Follow-up

    How do you exclude one auto-configuration?
    2IntermediateQuestionWhat is inside an executable Boot 3.2+ JAR?+

    Answer

    Main-Class is org.springframework.boot.loader.launch.JarLauncher, Start-Class is your application, bytecode is under BOOT-INF/classes, and libraries are nested in BOOT-INF/lib. Devtools stays quiet under java -jar.
    3IntermediateQuestionPort 8080 is already in use, or the route is 404. Where do you look?+

    Answer

    Override with SERVER_PORT or --server.port (the argument wins). A 404 with a live port usually means the controller sits outside the scan root.

    Hands-on exercise

    Lab. Add GET /api/orders/health returning record Health(String service, String status) with orders and UP. Run the fat JAR on SERVER_PORT=9090 and curl both routes. Move the controller to com.other, watch the routes disappear, and move it back.

    Summary

    Orders scans com.techlearningpro.orders, starts embedded Tomcat, and Jackson renders a record. Scan root, fat-JAR manifest, and an externalized port are the three facts every later lesson assumes.

    Ready to mark this lesson complete?Track your journey across the entire course.