VOOZH about

URL: https://www.geeksforgeeks.org/java/how-to-create-a-rest-api-using-java-spring-boot/

⇱ How to Create a REST API using Java Spring Boot - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Create a REST API using Java Spring Boot

Last Updated : 30 Apr, 2026

Creating a REST API using Spring Boot allows developers to build scalable and production-ready web services with minimal configuration. It simplifies RESTful service development by providing built-in support for HTTP methods, dependency injection, and auto-configuration.

  • Offers embedded servers (Tomcat/Jetty) to run applications without external deployment
  • Supports API testing using tools like Postman or Swagger UI
  • Allows easy configuration via application.properties or application.yml

Why Spring Boot?

Spring Boot makes it easy to create production-ready apps:

  • Built on top of Spring Framework
  • Reduces boilerplate code with auto-configurations
  • Quick setup for REST APIs
  • Beginner-friendly

Steps to Implements a REST API in Spring Boot

Follow the below steps to create and run a REST API in Spring Boot.

Step 1: Create the Spring Boot Project

Using Spring Initializr (Recommended)

1. Open Spring Initializr in your browser.
2. Fill in the project details:

  • Project: Maven
  • Language: Java
  • Spring Boot version: (latest stable, e.g., 3.1.x)
  • Group: com. example
  • Artifact: demo
  • Name: demo
  • Packaging: Jar
  • Java version: 17+

3. Click Add Dependencies-> Select Spring Web.
4. Click Generate-> It will download a .zip file.
5. Extract the zip-> Open it in your IDE (IntelliJ / Eclipse / VS Code).

👁 Structure
Project

Step 2: Define the Employee Entity

Create a simple Employee class inside com.example.demo:

Step 3: Create a Storage Class

Create a class to hold a list of employees.

Step 4: Create the DAO Class

The DAO class will handle adding and retrieving employees.

Step 5: Create the Controller

The controller contains the REST API endpoints.

Step 6: Run the Application

Open DemoApplication.java (already generated by Spring Boot) and run it:

Step 7: Testing the API

Now we can test the API using tools like Postman or a web browser.

GET Request: Fetch all employees

URL: http://localhost:8080/employees/

Response:

👁 Get-Request

POST Request: Add a new employee

Response:

👁 Post-Request

Again hitting the GET request after performing the POST request:

👁 GET-after-POST

This concludes creating a REST API using Spring Boot. Now we have a fully functioning API to manage employees.

Comment