Spring Framework Tutorial
Your First Spring Boot Web Application
Now the same container idea, but with Spring Boot: a real web application that answers HTTP requests. You will generate the project, add one controller, run it, call it with curl, and read the startup log line by line so that none of it looks like magic.
The project and every output below were produced with Spring Boot 4.1.1 on Java 17. The dependencies were Spring Web, Actuator and DevTools.
Step 1: generate and open the project
Create the project with Spring Initializr as shown in the previous lesson (Maven, Java, Spring Boot 4.1.1, dependencies web, actuator, devtools) and open it in your IDE. The generated HelloApplication class already contains everything Boot needs.
Step 2: add a REST controller
Create a class in the same package or a sub-package of the application class. @RestController combines @Controller and @ResponseBody, so returned values are written directly to the HTTP response. @GetMapping("/hello") maps GET requests for /hello to the method, and @RequestParam reads the query parameter name, with a default value when it is missing.
The package matters. @SpringBootApplication includes component scanning of its own package and everything below it, so a controller in a completely different package is silently ignored.
Step 3: run the application
You can run in three ways: click the run arrow in your IDE, run ./mvnw spring-boot:run, or build with ./mvnw package and run java -jar target/hello-0.0.1-SNAPSHOT.jar. A successful start prints the Spring Boot banner and log lines that end with "Started HelloApplication".
Step 4: call the endpoints
While the application runs, open http://localhost:8080/hello in a browser, or use curl. Because we added the Actuator starter, the health endpoint is also available. An unknown path returns HTTP 404. The exact responses are shown in the example.
Reading the startup log
Each log line tells a story. "Starting HelloApplication ... using Java 17.0.12" confirms the JDK. "No active profile set, falling back to 1 default profile" means no Spring profile was activated. "Tomcat initialized with port 8080" shows the embedded server; Boot 4.1.1 uses Tomcat 11.0, which implements Servlet 6.1. "Root WebApplicationContext: initialization completed in 1155 ms" is the container becoming ready. "Exposing 1 endpoint beneath base path '/actuator'" comes from the Actuator starter. Finally "Started HelloApplication in 2.557 seconds" means the application is ready.
What Spring Boot did for you
You wrote one controller, and Boot did the rest: it found the web starter on the classpath and auto-configured an embedded Tomcat, a DispatcherServlet, JSON conversion, error handling and static resource handling. You can see how it decided by starting the application with the debug flag (--debug), which prints the auto-configuration report.
Examples
HelloController.java
package com.webnest.hello;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(@RequestParam(defaultValue = "World") String name) {
return "Hello, " + name + "!";
}
}
Startup log (trimmed for width) from a real run
./mvnw spring-boot:run
# or: java -jar target/hello-0.0.1-SNAPSHOT.jar
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v4.1.1)
INFO ... [hello] com.webnest.hello.HelloApplication : Starting HelloApplication v0.0.1-SNAPSHOT using Java 17.0.12
INFO ... [hello] com.webnest.hello.HelloApplication : No active profile set, falling back to 1 default profile: "default"
INFO ... [hello] o.s.boot.tomcat.TomcatWebServer : Tomcat initialized with port 8080 (http)
INFO ... [hello] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/11.0.24]
INFO ... [hello] b.w.c.s.WebApplicationContextInitializer : Root WebApplicationContext: initialization completed in 1155 ms
INFO ... [hello] o.s.b.a.e.web.EndpointLinksResolver : Exposing 1 endpoint beneath base path '/actuator'
INFO ... [hello] o.s.boot.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path '/'
INFO ... [hello] com.webnest.hello.HelloApplication : Started HelloApplication in 2.557 seconds (process running for 3.031)
Calling the running application with curl
curl http://localhost:8080/hello
curl "http://localhost:8080/hello?name=Vansh"
curl http://localhost:8080/actuator/health
curl -s -o /dev/null -w "%{http_code}" http://localhost:8080/nope
Hello, World!
Hello, Vansh!
{"groups":["liveness","readiness"],"status":"UP"}
404
Common Mistakes
- Placing the controller in a package outside the application class package; it is never scanned and every request returns 404.
- Using @Controller instead of @RestController and getting a "template not found" error, because Spring tries to resolve a view named after the returned string.
- Running two instances at once. The second fails because port 8080 is already in use.
- Expecting changes to appear without a restart. Add Spring Boot DevTools or rebuild.
- Editing files under target. That folder is regenerated by every build.
Key Points to Remember
- One class with @SpringBootApplication plus one @RestController is a complete web application.
- Component scanning covers the application class package and all sub-packages.
- Boot 4.1.1 embeds Tomcat 11 and listens on port 8080 by default.
- The startup log shows the Java version, profile, port, server and total startup time.
Practice the examples
Change an input, predict the result, then compare it with the output. Explain why the result changes.
Use your local JDK or project IDE for these examples. Codelab currently runs Python and HTML/CSS/JavaScript; framework examples may need project dependencies.