Spring Boot offers a powerful and minimalistic way to build modern REST APIs in Java. Whether you’re just getting started with backend development or coming from a different stack, Spring Boot simplifies a lot of the boilerplate code.
In this guide, you’ll learn how to create First REST API in Spring Boot :
- Set up a Spring Boot project
- Create a simple RESTful controller
- Handle basic CRUD operations (GET, POST, PUT, DELETE)
- Test the API using Postman or curl
Letβs begin! π

β What is a REST API?
REST (Representational State Transfer) is an architectural style that uses standard HTTP methods like:
GET
β retrieve dataPOST
β create new dataPUT
β update existing dataDELETE
β remove data
Spring Boot makes it easy to implement these with annotations like @RestController
, @GetMapping
, @PostMapping
, etc.
βοΈ Step 1: Project Setup Using Spring Initializr
Go to https://start.spring.io and configure:
- Project: Maven
- Language: Java
- Spring Boot: Latest stable version
- Group:
com.kscodes.springboot
- Artifact:
firstapp
- Dependencies:
- Spring Web
- Spring Boot DevTools (optional for auto-reload)
- Lombok (optional for boilerplate reduction)
Click Generate, unzip the file, and open the project in your IDE (IntelliJ or Eclipse).
ποΈ Step 2: Project Structure
1 2 3 4 5 6 7 8 9 10 11 12 |
src/ βββ main/ βββ java/ β βββ com/kscodes/springboot/firstapp/ β βββ FirstAppApplication.java β βββ controller/ β βββ model/ β βββ service/ (optional for future enhancements) βββ resources/ βββ application.properties |
π¦ Step 3: Create the User Model
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package com.kscodes.springboot.firstapp.model; public class User { private Long id; private String name; private String email; public User() {} public User(Long id, String name, String email) { this.id = id; this.name = name; this.email = email; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } } |
π§ Step 4: Create the User Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
package com.kscodes.springboot.firstapp.controller; import com.kscodes.springboot.firstapp.model.User; import org.springframework.web.bind.annotation.*; import java.util.*; @RestController @RequestMapping("/api/users") public class UserController { private Map<Long, User> users = new HashMap<>(); private Long idCounter = 1L; // GET all users @GetMapping public List<User> getAllUsers() { return new ArrayList<>(users.values()); } // GET user by ID @GetMapping("/{id}") public User getUserById(@PathVariable Long id) { return users.get(id); } // POST new user @PostMapping public User createUser(@RequestBody User user) { user.setId(idCounter++); users.put(user.getId(), user); return user; } // PUT update user @PutMapping("/{id}") public User updateUser(@PathVariable Long id, @RequestBody User updatedUser) { User existing = users.get(id); if (existing != null) { existing.setName(updatedUser.getName()); existing.setEmail(updatedUser.getEmail()); } return existing; } // DELETE user @DeleteMapping("/{id}") public String deleteUser(@PathVariable Long id) { users.remove(id); return "User with ID " + id + " deleted."; } } |
βΆοΈ Step 5: Run the Application
Run the main class:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.kscodes.springboot.firstapp; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class FirstAppApplication { public static void main(String[] args) { SpringApplication.run(FirstAppApplication.class, args); } } |
π§ͺ Step 6: Test the API
You can test the API using Postman, curl, or any HTTP client.
πΉ Get All Users
1 2 3 4 5 |
GET http://localhost:8080/api/users |
πΉ Create a User
1 |
<br>POST http://localhost:8080/api/users<br>Content-Type: application/json<br><br>{<br> "name": "John Doe",<br> "email": "john@example.com"<br>}<br><br><br> |
πΉ Get User by ID
1 2 3 4 5 |
GET http://localhost:8080/api/users/1 |
πΉ Update a User
1 2 3 4 5 6 7 8 9 10 11 12 |
PUT http://localhost:8080/api/users/1 Content-Type: application/json { "name": "John S Doe", "email": "new@example.com" } |
πΉ Delete a User
1 2 3 4 5 |
DELETE http://localhost:8080/api/users/1 |
β Summary
You’ve successfully created your First REST API in Spring Boot that:
- Uses an in-memory map for storing users
- Supports full CRUD operations
- Can be extended to use databases or services in the future