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
- Visit https://start.spring.io/
- 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
- Add Dependencies:
- Spring Web
- Click “GENERATE” to download the project ZIP file
- 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
-
Open the project in your IDE
-
Examine the project structure:
pom.xml
: Contains dependencies and project configurationsrc/main/java/com/example/helloworld/HelloWorldApplication.java
: The main application classsrc/main/resources/application.properties
: Application configuration filesrc/test
: Contains test classes
-
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
- Find the main application class (
HelloWorldApplication.java
) - 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
-
Open a web browser and navigate to:
http://localhost:8080/
- Should display “Hello, World!”http://localhost:8080/greeting
- Should display “Welcome to Spring Boot!”
-
Alternatively, use cURL from a terminal:
curl http://localhost:8080/
curl http://localhost:8080/greeting
Step 6: Customize the Application
- Open
src/main/resources/application.properties
- Add the following line to change the server port:
server.port=8081
- Restart your application
- 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 thename
parameter from the query string to the method parameter - Spring Boot automatically converts the return value to JSON
Step 9: Test the New Endpoint
- Restart your application
- 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
-
Package the application:
Using Maven:
mvn package
Using Gradle:
./gradlew build
-
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) -
Test that the application works as expected by accessing the endpoints again
Step 11: Add Application Banner (Optional Fun Step)
- Create a file called
banner.txt
insrc/main/resources/
- Add ASCII art or text to personalize the startup banner, for example:
_ _ _ _ __ __ _ _
| | | | | | | \ \ / /__ _ __| | __| |
| |__| | ___| | | ___ \ \ /\ / / _ \| '__| |/ _` |
| __ |/ _ \ | |/ _ \ \ V V / (_) | | | | (_| |
| | | | __/ | | (_) | \_/\_/ \___/|_| |_|\__,_|
:: Spring Boot :: (v3.2.0)
- 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:
- What annotations did you use in this lab, and what is the purpose of each?
- How does Spring Boot convert Java objects to JSON?
- What is the significance of the
@SpringBootApplication
annotation? - How would you change the port number in a Spring Boot application?
- 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