First Application
A first Spring Boot 3 application on Java 21 is a walking skeleton.
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-parent3.5.x,java.version21, and the parent's-parametersflag so Jackson can bind records. Commitmvnw. - Scan: beans must live in
com.techlearningpro.ordersor 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-pluginrepackages onpackage. Do not launch*.jar.original.
Step-by-step explanation
- Generate with
spring-boot-starter-web, Java 21, groupcom.techlearningpro. - Place
OrdersApplicationincom.techlearningpro.ordersand keep the controller underneath it. - Run
./mvnw spring-boot:rununtilTomcat started on port 8080 (http). - Curl
/api/orders/42, then restart the fat JAR withSERVER_PORT=9090.
Informative example
Orders walking skeleton. Package-private types share this listing; split public types into their own files in the repo.
package com.techlearningpro.orders;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.bind.annotation.*;@SpringBootApplicationpublic 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, andinclude-binding-errorstonever. Pin them so a profile cannot flip them toalways. - Remote devtools (
spring.devtools.remote.secret) is remote code execution. Leave it off. Do not expose/actuator/env. - When
getOnereads 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
OrdersApplicationin the root package and application code beneath it. - Return a record per endpoint. Store money as minor units.
- Mark
spring-boot-devtoolsoptional. Repackage leaves it out of the fat JAR, andjava -jardisables restart. - Log order ids. Leave customer payloads off the info line.
Common mistakes
- A
mainclass 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 isjakarta.*for servlet, validation, and JPA. - Hardcoding 8080 in a
TomcatServletWebServerFactory.SERVER_PORTand--server.portalready exist, and the argument wins.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
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
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
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.