Lecture 2 — First Spring Boot application

Spring Initializr, embedded Tomcat, Hello World REST

Author

Practice notes based on Aditya Tandon (Coder Army)

Video: Build Your First Spring Boot Application | REST API | Full Course #2
Playlist index: 2


Goal

Build a tiny Spring Boot app that responds to:

GET http://localhost:8080/hello  →  Hello World

You will type every step in your IDE. Pause the video and implement here — that is the point of these notes.


Concepts you need while coding

Localhost, IP, and port

Idea Plain meaning
IP address Unique address of a machine on a network
localhost / 127.0.0.1 “This same computer”
Port Number that routes traffic to one process on that machine (Chrome, Spotify, your app…)
Default Spring Boot port 8080 (changeable)

On the public internet, browsers often hit 443 (HTTPS). A reverse proxy may forward that to your app’s internal port (e.g. 8080). Locally you call the app port directly.

Spring Initializr

start.spring.io generates a ready project skeleton: folders, Java version, Spring Boot version, and pom.xml dependencies — so you are not stuck on setup before writing business code.

Embedded Tomcat

Adding Spring Web pulls in an embedded Apache Tomcat. When you run the main class, Tomcat listens on a port. You do not install Tomcat separately for this hello-world.

Annotations you will use (names only — depth later)

Annotation Role in this lecture
@SpringBootApplication On the generated main class — Boot entry point
@RestController Marks a class as an HTTP API “gateway”
@GetMapping("/hello") Maps HTTP GET /hello to a method

Practice — build it step by step

Step 0 — prerequisites

  • JDK installed
  • IDE ready
  • Video paused after the Initializr section if you prefer typing first

Step 1 — generate the project

  1. Open https://start.spring.io
  2. Choose roughly:
Field Suggested choice
Project Maven
Language Java
Spring Boot A stable release (avoid SNAPSHOT / RC while learning)
Packaging Jar
Java 21 (or 17+)
Dependencies Spring Web
  1. Click Generate, unzip the project, open the folder in your IDE.
Note

Exact Boot version numbers change over time. Prefer the newest stable release Initializr offers. Concepts stay the same.

Step 2 — run the skeleton once

Open the generated *Application.java (has public static void main).

Optionally add one line inside main before understanding Boot:

System.out.println("Hello World");

Run the main class. In the console logs you should see something like Tomcat started on port(s): 8080.

Open a browser:

http://localhost:8080

You will likely see a Whitelabel Error Page — that is OK. It proves the server is up, but no endpoint is mapped yet.

Step 3 — create HelloController

In the same package as your main application class (e.g. com.example.demo), create:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        return "Hello World";
    }
}

What you are doing

  • @RestController → “this class exposes HTTP endpoints”
  • @GetMapping("/hello") → “when someone GETs /hello, run this method”
  • Returning a String → Spring writes that string as the HTTP response body

Step 4 — verify in the browser

  1. Re-run the application (restart so changes load).
  2. Visit:
http://localhost:8080/hello

Expected: page shows Hello World.

Step 5 — change the port

Open src/main/resources/application.properties and add:

server.port=9090

Restart. Tomcat should start on 9090. Now:

http://localhost:8080/hello   → fails (nothing listening)
http://localhost:9090/hello   → Hello World

Set the port back to 8080 (or delete the line) when you are done experimenting.

Step 6 — second endpoint

Add another method (method name does not have to match the path):

@GetMapping("/bye")
public String greetBye() {
    return "Bye";
}

Restart and test both /hello and /bye.

Step 7 — optional HTML in the response

@GetMapping("/hello")
public String hello() {
    return "<h1>Hello World</h1>";
}

The browser will render the heading. (Real apps usually return JSON and let a frontend render HTML — that comes later.)


Mental model after this lecture

Browser  --GET /hello-->  Embedded Tomcat (port 8080)
                              |
                              v
                         Spring MVC mapping
                              |
                              v
                         HelloController.hello()
                              |
                              v
                         "Hello World" response body

You have not yet learned how mapping works internally. That is intentional. Lecture 1–2 show the destination; later lectures open the engine.


Exercises

  1. Add GET /ping that returns pong.
  2. Change server.port to 7070 and update your browser URL.
  3. Temporarily remove @RestController, restart, hit /hello — what happens? Put it back.
  4. Write three bullet points: What did Initializr save you from doing by hand?

Self-check

  1. Why do you hit localhost in this lecture instead of a public domain?
  2. Who starts Tomcat when you run the main class?
  3. Does the Java method name have to equal the URL path?

Answers: self-check-answers