How to Build a REST API with Java Spring Boot – A Step‑by‑Step Walkthrough
- Nishadil
- September 09, 2026
- 0 Comments
- 3 minutes read
- 6 Views
- Save
- Follow Topic
Turn a fresh Spring Boot project into a working Employee REST service in minutes
A hands‑on guide that walks you through creating a Spring Boot REST API, from project scaffolding with Spring Initializr to defining models, a DAO, and a controller.
If you’ve ever wondered how to spin up a RESTful web service in Java without drowning in configuration files, Spring Boot is the answer. It bundles an embedded server, auto‑configures the heavy lifting, and lets you focus on the actual business logic.
First things first – you need a project skeleton. The easiest way is to visit Spring Initializr. Pick Maven as the build tool, Java 17 (or newer), and add the Spring Web dependency. Hit “Generate”, unzip the bundle, and open it in your favorite IDE – whether that’s IntelliJ, Eclipse, or VS Code.
When the project loads, you’ll see the classic DemoApplication.java in the root package. Keep it there; Spring Boot will automatically scan any sub‑packages you create, which is exactly what we want for a clean, layered architecture.
Next, let’s sketch out the data model. Inside src/main/java/com/example/demo/model create an Employee class. It holds four fields – id, firstName, lastName, and email. Add a no‑arg constructor, a full‑argument one, getters, setters, and a friendly toString(). Nothing fancy, just plain Java beans.
Because we aren’t connecting to a real database for this demo, we’ll keep the employees in memory. Add another class called Employees in the same package; it simply wraps an ArrayList<Employee> and exposes the list via getter and setter methods.
Now comes the data‑access layer. In src/main/java/com/example/demo/dao, drop an EmployeeDAO class and annotate it with @Repository. Inside, instantiate the Employees container and pre‑populate it with a few sample records – maybe “Prem”, “Vikash”, and “Ritesh”. The DAO will expose three handy methods: getAllEmployees(), getEmployeeById(Integer id), and addEmployee(Employee employee). The latter just pushes a new object onto the list.
With the backend ready, it’s time for the controller. Create EmployeeController under com.example.demo.controller and mark it with @RestController. Map the class to /api/employees using @RequestMapping. Then add three endpoints:
@GetMapping– returns the whole employee list.@GetMapping("/{id}")– fetches a single employee by ID, returningResponseEntitywithHttpStatus.NOT_FOUNDif absent.@PostMapping– consumes a JSON payload, hands it to the DAO, and replies withHttpStatus.CREATED.
Because Spring Boot ships with an embedded Tomcat, you can simply run mvn spring-boot:run (or click the run button in your IDE). The application starts on port 8080 by default. Fire up Postman or Swagger UI, hit GET http://localhost:8080/api/employees, and you should see the three dummy employees you seeded earlier.
That’s pretty much it. You now have a functional REST API, built with Spring Boot, that you can extend with persistence, validation, or security whenever you’re ready.
- India
- News
- Technology
- TechnologyNews
- Model
- Controller
- Java
- Dao
- DependencyInjection
- JavaSpring
- SpringBoot
- RestApi
- DaoClass
- ScalableWebServices
- EmployeeEntity
- JavaAdvanced
- SpringInitializr
- RestFramework
- AutoConfiguration
- PostmanTesting
- ApplicationProperties
- WebApi
- ProductionReadyApplications
- JavaVersion17
- EmbeddedServers
- ApiEndpoints
- EmployeeManagementSystem
- EmployeeApi
Editorial note: Nishadil may use AI assistance for news drafting and formatting. Readers can report issues from this page, and material corrections are reviewed under our editorial standards.