🧮 Lab 5 RESTful Web Services
Duration: ~1 hour
Environment: Windows, Linux, or macOS
🎯 Building a RESTful Web Service
This guide walks you through creating a simple “Hello, World” RESTful web service with Spring Boot.
Spring Boot is an open-source framework used to build standalone, production-grade Spring applications with minimal configuration.
Its flexibility allows you to bundle applications as fat/uber JARs, making deployment as simple as running a single .jar file — an excellent choice for developing microservices in Java.
Spring is the programmer’s favorite application framework for enterprise Java development, and REST is the de facto standard for building web APIs.
💡 This guide and many others can be found in the Getting Started Spring Guides.
⚙️ What You Will Build
You will build a service that accepts HTTP GET requests at:
http://localhost:8080/greeting
It will respond with a JSON representation of a greeting:
{"id":1,"content":"Hello, World!"}
You can customize the greeting with an optional name parameter in the query string:
http://localhost:8080/greeting?name=User
Response:
{"id":1,"content":"Hello, User!"}
🧩 Required Software
Make sure you have installed the software listed on the Required Software page.
Step 1 — Create a Spring Project
To initialize the project:
- Navigate to https://start.spring.io.
- Choose:
- Project: Maven
- Language: Java
- Packaging: JAR
- Java Version: 11 (or your installed version)
- Click Dependencies → select Spring Web.
- Click Generate to download the project ZIP file.
- Extract the archive — it contains a web application pre-configured with your choices.
Step 2 — Import the Project into Your IDE
You can use any IDE. This example uses Eclipse:
- Extract the ZIP file into a folder.
- Open Eclipse → File → Import → Maven → Existing Maven Projects.
- Navigate to the extracted project folder → click OK to import.
Step 3 — Creating the REST Endpoint
Create a Resource Representation Class
Your service will handle GET requests for /greeting, optionally with a name parameter.
The response will look like:
{
"id": 1,
"content": "Hello, World!"
}
Model class (Greeting.java):
package com.example.restservice;
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;
}
}
🧩 The application uses Jackson, included by default in Spring Boot, to automatically convert Java objects to JSON.
Create a Resource Controller
In Spring, HTTP requests are handled by a controller identified by the @RestController annotation.
Controller class (GreetingController.java):
package com.example.restservice;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GreetingController {
private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();
@GetMapping("/greeting")
public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
return new Greeting(counter.incrementAndGet(), String.format(template, name));
}
}
Explanation
@GetMapping("/greeting")maps HTTPGETrequests to thegreeting()method.@RequestParambinds thenamequery parameter to the method argument.- If absent,
defaultValue = "World"is used.
- If absent,
- The method returns a new
Greetingobject with a unique ID and formatted content.
🧠
@RestControlleris a shorthand for combining@Controllerand@ResponseBody.
Spring automatically converts the returnedGreetingobject into JSON usingMappingJackson2HttpMessageConverter.
Step 4 — Test Your Spring RESTful Web Service
Run the Application class as a Java Application.
You should see logs similar to:
Tomcat started on port(s): 8080 (http) with context path ''
This means your Spring Boot application is running on an embedded Tomcat server at port 8080.
Build an Executable JAR
If you use Maven, you can run the app directly:
./mvnw spring-boot:run
Or build and run the JAR:
./mvnw clean package
java -jar target/gs-rest-service-0.1.0.jar
Test the Service
Visit:
http://localhost:8080/greeting
Output:
{"id":1,"content":"Hello, World!"}
Now try:
http://localhost:8080/greeting?name=Georgios
Output:
{"id":2,"content":"Hello, Georgios!"}
Other Materials
Continue exploring: