Hello REST API with Spring Boot
Before you start
In IntelliJ, create a new Spring Boot 4 project that uses JDK25 and Java 25 with the only dependency being Spring Web.
Exercise 1 - Hello REST API
Goal: Understand what a REST endpoint is, what JSON looks like, and how Spring serializes objects.
-
Create a new class named
HelloController, and add theGET /api/helloendpoint that returns JSON:{"message": "Hello, World!","timestamp": "2026-01-28T10:00:00"}Replace the timestamp value with the current date and time, by using
LocalDateTime.now().Hint: Use a
Map<String, Object>to create the JSON response. -
Test the endpoint using your browser or Postman by navigating to
http://localhost:8080/api/hello. -
In the same class, add another GET endpoint
/api/greet/{name}that takes a path variablenameand returns a JSON response:{"message": "Hello, {name}!","timestamp": "2026-01-28T10:00:00"}Replace
{name}with the actual name provided in the URL and the timestamp value with the current date and time. -
Test the endpoint using your browser or Postman by navigating to
http://localhost:8080/api/greet/YourName. ReplaceYourNamewith any name you choose. -
Add a
GET /api/echo?text=...returning the text provided as a query parameter:{"echo": "Your text here"}Replace
Your text herewith the actual text provided in the query parameter and the timestamp value with the current date and time. -
Test the endpoint using your browser or Postman by navigating to
http://localhost:8080/api/echo?text=Hello. -
Instead of using a
HashMap, create a new class namedGreetingResponsewith fieldsmessageandtimestamp. Modify the/api/helloand/api/greet/{name}endpoints to return instances ofGreetingResponseinstead of aHashMap.