Building a Hello World REST API with Spring Boot
Spring Boot makes it incredibly easy to create production-ready REST APIs. In this tutorial, we’ll build a simple “Hello World” REST API from scratch.
💡 New to Spring Boot? This tutorial is perfect for beginners. We’ll walk through every step to create your first REST API in just a few minutes!
Prerequisites
Before we start, make sure you have:
- Java 17 or higher installed
- Maven or Gradle
- Your favorite IDE (IntelliJ IDEA, Eclipse, or VS Code)
Step 1: Create a New Spring Boot Project
The easiest way to create a Spring Boot project is using Spring Initializr:
- Go to start.spring.io
- Choose Maven Project
- Select Java 17
- Add dependency: Spring Web
- Click Generate to download the project
Alternatively, you can use the Spring Boot CLI or create it directly in your IDE.
Step 2: Project Structure
After extracting the downloaded zip, your project structure should look like this:
hello-world-api/
├── src/
│ └── main/
│ └── java/
│ └── com/example/demo/
│ └── DemoApplication.java
├── pom.xml
└── application.properties
Step 3: Create a REST Controller
Create a new file called HelloController.java in the same package as your main application class:
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello, World!";
}
@GetMapping("/greet")
public Greeting greet() {
return new Greeting("Hello from Spring Boot!");
}
}
Step 4: Create a Response Model
Let’s create a simple model class for structured responses:
package com.example.demo;
public class Greeting {
private String message;
private long timestamp;
public Greeting(String message) {
this.message = message;
this.timestamp = System.currentTimeMillis();
}
// Getters and setters
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public long getTimestamp() {
return timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
}
Step 5: Run Your Application
Run your Spring Boot application:
mvn spring-boot:run
Or if you’re using Gradle:
./gradlew bootRun
Your application will start on port 8080 by default.
⚠️ Port Already in Use? If you see an error about port 8080 being in use, either stop the other application or change the port in application.properties (see the Customizing the Port section below).
Step 6: Test Your API
Open your browser or use curl to test the endpoints:
Simple String Response
curl http://localhost:8080/api/hello
Response:
Hello, World!
JSON Response
curl http://localhost:8080/api/greet
Response:
{
"message": "Hello from Spring Boot!",
"timestamp": 1700000000000
}
Understanding the Annotations
Let’s break down what each annotation does:
@RestController: Combines@Controllerand@ResponseBody, indicating this class handles REST requests@RequestMapping("/api"): Sets the base path for all endpoints in this controller@GetMapping("/hello"): Maps HTTP GET requests to the specified method
Adding More Endpoints
You can easily add more endpoints with different HTTP methods:
@PostMapping("/create")
public String create(@RequestBody String data) {
return "Created: " + data;
}
@PutMapping("/update/{id}")
public String update(@PathVariable Long id) {
return "Updated item: " + id;
}
@DeleteMapping("/delete/{id}")
public String delete(@PathVariable Long id) {
return "Deleted item: " + id;
}
Customizing the Port
If you want to run on a different port, add this to application.properties:
server.port=9090
What’s Next?
Now that you have a basic REST API running, you can:
- Add a database with Spring Data JPA
- Implement proper error handling
- Add validation to your endpoints
- Secure your API with Spring Security
- Write unit tests for your controllers
Conclusion
Spring Boot makes it incredibly simple to create REST APIs. With just a few lines of code, you have a fully functional web service. The framework handles all the boilerplate, letting you focus on your business logic.
Happy coding!
Comments
Join the discussion and share your thoughts