Lab: Create a minimal Spring Boot web app that prints 'Hello, World'


Objective: Understand the core annotation and bootstrap a Spring Boot app.

Prerequisites

  • JDK 17 or higher installed
  • Maven 3.6+ or Gradle 7.0+ installed
  • A code editor or IDE (IntelliJ IDEA, Eclipse, or VS Code recommended)

Step 1: Generate a Spring Boot Project

You have two options:

Option A: Using Spring Initializr Web Interface

  1. Visit https://start.spring.io/
  2. Configure your project:
    • Project: Maven Project
    • Language: Java
    • Spring Boot: 3.2.x (or latest stable)
    • Group: com.example
    • Artifact: hello-world
    • Name: hello-world
    • Description: A simple Hello World Spring Boot application
    • Package Name: com.example.helloworld
    • Packaging: Jar
    • Java Version: 17
  3. Add Dependencies:
    • Spring Web
  4. Click “GENERATE” to download the project ZIP file
  5. Extract the ZIP file to your workspace

Option B: Using Command Line (Maven)

Run the following cURL command:

curl https://start.spring.io/starter.zip \
  -d type=maven-project \
  -d language=java \
  -d bootVersion=3.2.0 \
  -d baseDir=hello-world \
  -d groupId=com.example \
  -d artifactId=hello-world \
  -d name=hello-world \
  -d description="A simple Hello World Spring Boot application" \
  -d packageName=com.example.helloworld \
  -d packaging=jar \
  -d javaVersion=17 \
  -d dependencies=web \
  -o hello-world.zip

Then extract the ZIP file:

unzip hello-world.zip -d ./

Step 2: Open and Explore the Project

  1. Open the project in your IDE

  2. Examine the project structure:

    • pom.xml: Contains dependencies and project configuration
    • src/main/java/com/example/helloworld/HelloWorldApplication.java: The main application class
    • src/main/resources/application.properties: Application configuration file
    • src/test: Contains test classes
  3. Look at the main application class:

package com.example.helloworld;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HelloWorldApplication {
    public static void main(String[] args) {
        SpringApplication.run(HelloWorldApplication.class, args);
    }
}

Note the @SpringBootApplication annotation, which combines:

  • @Configuration
  • @EnableAutoConfiguration
  • @ComponentScan

Step 3: Create a Controller

Create a new Java class called HelloController.java in the same package as your main application class:

package com.example.helloworld;

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

@RestController
public class HelloController {

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

    @GetMapping("/greeting")
    public String greeting() {
        return "Welcome to Spring Boot!";
    }
}

Key points:

  • @RestController: Marks the class as a controller where each method returns a domain object instead of a view
  • @GetMapping("/"): Maps HTTP GET requests to the root path to the hello() method

Step 4: Run the Application

Option A: Using IDE

  1. Find the main application class (HelloWorldApplication.java)
  2. Right-click on it and select “Run” or “Debug”

Option B: Using Maven

From the project directory, run:

mvn spring-boot:run

Option C: Using Gradle

From the project directory, run:

./gradlew bootRun

Step 5: Test the Application

  1. Open a web browser and navigate to:

    • http://localhost:8080/ - Should display “Hello, World!”
    • http://localhost:8080/greeting - Should display “Welcome to Spring Boot!”
  2. Alternatively, use cURL from a terminal:

curl http://localhost:8080/
curl http://localhost:8080/greeting

Step 6: Customize the Application

  1. Open src/main/resources/application.properties
  2. Add the following line to change the server port:
server.port=8081
  1. Restart your application
  2. Now test using the new port: http://localhost:8081/

Step 7: Add a Model Class

Create a new package com.example.helloworld.model and add a Greeting.java class:

package com.example.helloworld.model;

public class Greeting {
    private final long id;
    private final String content;

    public Greeting(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }
}

Step 8: Update the Controller to Use the Model

Modify your HelloController.java to include a new endpoint that returns a Greeting object:

package com.example.helloworld;

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

import com.example.helloworld.model.Greeting;

import java.util.concurrent.atomic.AtomicLong;

@RestController
public class HelloController {

    private static final String TEMPLATE = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

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

    @GetMapping("/greeting")
    public String greeting() {
        return "Welcome to Spring Boot!";
    }

    @GetMapping("/api/greeting")
    public Greeting greetingJson(@RequestParam(value = "name", defaultValue = "World") String name) {
        return new Greeting(counter.incrementAndGet(), String.format(TEMPLATE, name));
    }
}

Key Points:

  • We’ve added a new endpoint /api/greeting that returns a Greeting object
  • The @RequestParam annotation binds the name parameter from the query string to the method parameter
  • Spring Boot automatically converts the return value to JSON

Step 9: Test the New Endpoint

  1. Restart your application
  2. Test the new endpoint:
    • http://localhost:8081/api/greeting
    • http://localhost:8081/api/greeting?name=Student

When accessing the endpoint with a browser or cURL, you should see JSON output like:

{ "id": 1, "content": "Hello, World!" }

And with the name parameter:

{ "id": 2, "content": "Hello, Student!" }

Step 10: Package and Run as JAR

  1. Package the application:

    Using Maven:

    mvn package

    Using Gradle:

    ./gradlew build
  2. Run the packaged JAR:

    java -jar target/hello-world-0.0.1-SNAPSHOT.jar

    (For Gradle, the JAR will be in the build/libs/ directory)

  3. Test that the application works as expected by accessing the endpoints again

Step 11: Add Application Banner (Optional Fun Step)

  1. Create a file called banner.txt in src/main/resources/
  2. Add ASCII art or text to personalize the startup banner, for example:
  _    _      _ _         __        __         _     _
 | |  | |    | | |        \ \      / /__  _ __| | __| |
 | |__| | ___| | | ___     \ \ /\ / / _ \| '__| |/ _` |
 |  __  |/ _ \ | |/ _ \     \ V  V / (_) | |  | | (_| |
 | |  | |  __/ | | (_) |     \_/\_/ \___/|_|  |_|\__,_|

 :: Spring Boot ::                      (v3.2.0)
  1. Restart your application to see the custom banner

Step 12: Document Your Understanding

In your project directory, create a file called NOTES.md with answers to the following:

  1. What annotations did you use in this lab, and what is the purpose of each?
  2. How does Spring Boot convert Java objects to JSON?
  3. What is the significance of the @SpringBootApplication annotation?
  4. How would you change the port number in a Spring Boot application?
  5. What are the advantages of using Spring Boot compared to traditional Java web applications?

Conclusion

Congratulations! You’ve successfully:

  • Created a Spring Boot application from scratch
  • Implemented a RESTful controller with multiple endpoints
  • Used a model class and parameter handling
  • Packaged and run the application as a standalone JAR

By Wahid Hamdi